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

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
WBOYOriginal
2016-10-19 10:40:521204browse

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>
<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>
<code>输出结果:str:, buf length:0, len:
</code>

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>
<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>
<code>输出结果:str:, buf length:0, len:
</code>

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.

Statement:
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