doulan8330 2012-12-08 10:26
浏览 146

PHP json_encode在json-string的开头添加了一些数字(十六进制?)

I am echoing json_encoded data from one php script to another (the request is made by fsockopen/GET).

When having encoded an array with 40 elements, there is no problem. When doing exactly the same thing with 41, some numbers and is added to the beginning of the json string.

This is the beginning of the string just before I echo it:

{"transactions":[{"transaction_id":"03U191739F337671L",

This is how I send the data:

header('Content-Type: text/plain; charset=utf-8');
error_log(json_encode($transaction_list));
echo json_encode($transaction_list);

As soon as I have received the data in the requesting script I print it again to error_log:

27fc
{"transactions":[{"transaction_id":"03U191739F337671L",

The "27fc " is not there if I retrieve less data.

This is how I handle the response:

$response="";
 while (!feof($fp)) {
            $response .= fgets($fp, 128);
 }

 //Seperate header and content
 $separator_position = strpos($response,"

");      

 $header_text = substr($response,0,$separator_position);      

 $body = substr($response,$separator_position+4);                   
 error_log($body);  

 fclose($fp);   

I have tried playing around with the time out of the fsockopen request, that doesn't matter. The same thing with max_execution_time and max_input_time in php.ini, doesn't matter. I was thinking that the content in some way may have been cut due to time out...

The 41st array is having no different format of the content than the preceding ones.

What is happening and how can I fix it?

I am using Linux, Apache (httpd) and PHP.

UPDATE

The data seems to be chunked. In the response, following header is included: "Transfer-Encoding: chunked".

  • 写回答

1条回答 默认 最新

  • duanlipeng4136 2012-12-08 12:01
    关注

    Based on @Salmans idea of using file_get_contents, this is the working solution. This uses POST to send the data (GET didn't seem to be working, I think one has to append that query string to the URL oneself):

    $postdata = http_build_query(         
       array('customer_id' => $customer_id)
    );
    
    $opts = array('http' =>
       array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => $postdata
       )
    );
    
    $context  = stream_context_create($opts);  
    $content = file_get_contents($my_url, false, $context);
    return $content;
    
    评论

报告相同问题?

悬赏问题

  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害