0

我将 json_encoded 数据从一个 php 脚本回显到另一个(请求由 fsockopen/GET 发出)。

当对一个包含 40 个元素的数组进行编码时,没有问题。当用 41 做同样的事情时,一些数字和 \r\n 被添加到 json 字符串的开头。

这是我回显之前字符串的开头:

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

这就是我发送数据的方式:

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

一旦我收到请求脚本中的数据,我就会再次将其打印到 error_log:

27fc\r\n{"transactions":[{"transaction_id":"03U191739F337671L",

如果我检索的数据较少,则“27fc\r\n”不存在。

这就是我处理响应的方式:

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

 //Seperate header and content
 $separator_position = strpos($response,"\r\n\r\n");      

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

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

 fclose($fp);   

我试过玩弄 fsockopen 请求的时间,没关系。与 php.ini 中的 max_execution_time 和 max_input_time 相同,没关系。我在想,由于超时,内容可能以某种方式被剪掉了……

第 41 个数组的内容格式与前面的没有不同。

发生了什么,我该如何解决?

我正在使用 Linux、Apache (httpd) 和 PHP。

更新

数据似乎是分块的。在响应中,包含以下标头:“Transfer-Encoding: chunked”。

4

1 回答 1

0

基于@Salmans 使用 file_get_contents 的想法,这是可行的解决方案。这使用 POST 发送数据(GET 似乎没有工作,我认为必须自己将该查询字符串附加到 URL):

$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;
于 2012-12-08T12:01:00.563 回答