【问题标题】:how to combine json encoded arrays returned from parallelcurl in PHP如何在PHP中组合从parallelcurl返回的json编码数组
【发布时间】:2012-06-12 17:35:16
【问题描述】:

我正在尝试实施我在此站点上找到的解决方案:

Parallel cURL execution in PHP

但是,我在合并所有结果并将其发送到 xmlhttprequest 进行处理时遇到了问题。最初 xmlhttprequest 将调用一个并行执行其他 php 脚本的 php 脚本。这是我的主要 php 脚本(由 xmlhttprequest 调用):

//parallelcurl_index.php

$param1 = $_REQUEST['param1'];
$param2 = $_REQUEST['param2'];


require_once('parallelcurl.php');

$url1 = "http://example.com/script1.php?param1=" . $param1 . "&param2=" . $param2;
$url2 = "http://example.com/script2.php?param1=" . $param1 . "&param2=" . $param2;
$url3 = "http://example.com/script3.php?param1=" . $param1 . "&param2=" . $param2;
$url4 = "http://example.com/script4.php?param1=" . $param1 . "&param2=" . $param2;
$url5 = "http://example.com/script5.php?param1=" . $param1 . "&param2=" . $param2;
$url6 = "http://example.com/script6.php?param1=" . $param1 . "&param2=" . $param2;
/* 
 each of the above urls will execute one or more oracle sql queries and procedures and store the results in array which 
 will be sent to this script.
 example $url1 will send results like this:

$url1_response = array('city' => $city, 'country' => $country);
echo json_encode($url1_response); 

*/

$max_requests = 10;

$curl_options = array(
    CURLOPT_SSL_VERIFYPEER => FALSE,
    CURLOPT_SSL_VERIFYHOST => FALSE
);

$parallel_curl = new ParallelCurl($max_requests, $curl_options);

// Start 6 parallel requests. All three will be started simultaneously.
$parallel_curl->startRequest($url1, 'on_request_done');
$parallel_curl->startRequest($url2, 'on_request_done');
$parallel_curl->startRequest($url3, 'on_request_done');
$parallel_curl->startRequest($url4, 'on_request_done');
$parallel_curl->startRequest($url5, 'on_request_done');
$parallel_curl->startRequest($url6, 'on_request_done');

$parallel_curl->finishAllRequests();


// This function gets called back for each request that completes
function on_request_done($content, $url, $ch, $search) {
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    if ($httpcode !== 200) {
        print "Fetch error $httpcode for '$url'\n";
        return;
    }

    $responseobject = json_decode($content, true);

     // I tried this, to combine the arrays but did not work:
     //$segments = array();
     //$segments = array_merge($responseobject, $segments);
     //echo json_encode($segments);


    // send results from all above scripts to xmlhttprequest 
    echo json_encode($responseobject); 

}

回到 xmlhttprequest ,我可以检查所有返回的数据:

alert(xmlHttp.responseText);                  

alert() 输出如下:

{"city":"Muscat","country":"Oman"}{"company":"OTL","Department":"IT"}

我可以看到每个 json 数组之间没有逗号分隔符,因此 eval() 将失败:

 var  responseArr = eval('(' + xmlHttp.responseText + ')');

如果我只在 $parallel_curl->startRequest() 中为一个 url 运行脚本并评论其他五个它可以正常工作。 任何帮助将不胜感激。

【问题讨论】:

    标签: php json xmlhttprequest


    【解决方案1】:

    嗯,我不是粉丝或并行 curl 请求(为什么要绑定网络服务器,像 GearMan 这样的东西更适合),但如果你走这条路:

    class ReturnCollector {
       private $data = array();
       function addData($content){
          $this->data[] = json_decode($content, true);
       }
       function getData(){
          return $this->data);
       }
       function outputData(){
          echo json_encode($this->getData());
       }
    }
    
    
    $collector = new ReturnCollector();
    $parallel_curl->startRequest($url1, array($collector,'addData'));
    $parallel_curl->startRequest($url2, array($collector,'addData'));
    $parallel_curl->startRequest($url3, array($collector,'addData'));
    //etc...
    $parallel_curl->finishAllRequests();
    $collector->outputData();
    

    【讨论】:

    • 我认为实际上使该对象收集某些东西(而不仅仅是选择最后一个)缺少一些东西。请检查编辑。
    • 啊,是的,这确实是我打算写的,谢谢@hakre。
    【解决方案2】:

    我已经修改了课程代码:

    class ReturnCollector {
        private $data     = array();
        private $tmpArr   = array();
        function addData($content){
            $this->tmpArr   = json_decode($content, true);
            $this->data = array_merge($this->tmpArr, $this->data);
        }
        function getData(){
            return $this->data;
        }
        function outputData(){
            echo json_encode($this->getData());
        }
    }
    

    现在一切正常,非常感谢您的帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-09
      • 2015-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多