PHP extension development - PHP uses C language to develop extensions: When the zend_parse_parameters method receives binary data, it cannot get the data?

WBOY
Release: 2016-10-19 10:40:52
Original
1203 people have browsed it

In PHP extension development, there is a method that needs to receive binary data. zend_parse_parameters sets the parameter type to "s". When the parameters are printed, they are empty. It seems that they are not received.

<code class="c">PHP_FUNCTION(hau_socket)
{
    char *buf;
    int len;


    if( zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC, "s", &buf, &len) == FAILURE ) {
        return;
    }

    php_printf("str:%s, buf length:%d, len:\n", buf, strlen(buf), len);

}</code>
Copy after login
Copy after login
<code><?php

$packet = new SocketPacket();//使用pack函数打包进制数据
$packet->WriteBegin( 0x7070 );
$packet->WriteInt_N( 0x1001 );
$packet->WriteInt_N( 1 );
$packet->WriteInt_N(9055249);
$packet->WriteEnd_n();
hau_socket( $packet->GetPacketBuffer() );</code>
Copy after login
Copy after login
<code>输出结果:str:, buf length:0, len:
</code>
Copy after login
Copy after login

Is the parameter type setting incorrect? ?

Reply content:

In PHP extension development, there is a method that needs to receive binary data. zend_parse_parameters sets the parameter type to "s". When the parameters are printed, they are empty. It seems that they are not received.

<code class="c">PHP_FUNCTION(hau_socket)
{
    char *buf;
    int len;


    if( zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC, "s", &buf, &len) == FAILURE ) {
        return;
    }

    php_printf("str:%s, buf length:%d, len:\n", buf, strlen(buf), len);

}</code>
Copy after login
Copy after login
<code><?php

$packet = new SocketPacket();//使用pack函数打包进制数据
$packet->WriteBegin( 0x7070 );
$packet->WriteInt_N( 0x1001 );
$packet->WriteInt_N( 1 );
$packet->WriteInt_N(9055249);
$packet->WriteEnd_n();
hau_socket( $packet->GetPacketBuffer() );</code>
Copy after login
Copy after login
<code>输出结果:str:, buf length:0, len:
</code>
Copy after login
Copy after login

Is the parameter type setting incorrect? ?

No problem was found in the use of zend_parse_parameters, and you did not report a null pointer when printf, indicating that buf was indeed assigned. I think you should check if $packet->GetPacketBuffer() returns a string.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!