double820122 2012-06-11 19:49
浏览 41
已采纳

如何组合PHP中parallelcurl返回的json编码数组

I am trying to implement a soluion I found at this site:

Parallel cURL execution in PHP

however, I have a problem to combine all results and send it to xmlhttprequest to process it. initially xmlhttprequest will call a php script which executes other php scripts in parallel. here is my main php script (called by 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'
";
        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); 

}

back at the xmlhttprequest , I can check for all the data returned:

alert(xmlHttp.responseText);                  

alert() output like this:

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

I can see there is no coma separator between each json array, hence eval() will fail:

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

if I run the script for one url only in $parallel_curl->startRequest() and comment the other five it work without any problems. any help will be appreciated.

  • 写回答

2条回答 默认 最新

  • douzhongju8780 2012-06-11 21:31
    关注

    Hm, I am NOT a fan or parallel curl request (why tie up the webserver, stuff like GearMan is better suited), but if you are going this route:

    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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)