在PHP中使用curl时无法获得任何响应

时间:2018-08-31 11:47:46

标签: php curl

我正在使用php中的curl来获取数据,但是由于存在许多数据,因此返回0输出。我在下面提供我的代码。

 $result = array();
// multi handle
$mh = curl_multi_init();
$idArr=[2,147,92];
foreach ($idArr as $key => $value) {
    $fetchURL = "http://example.com/index.php/rest/V1/categories/".$value."/products/";
    //echo $fetchURL.'<br>';
    $multiCurl[$key] = curl_init();
    curl_setopt($multiCurl[$key], CURLOPT_URL,$fetch_url);
    curl_setopt($multiCurl[$key], CURLOPT_CUSTOMREQUEST, "GET");
    curl_setopt($multiCurl[$key], CURLOPT_HEADER,array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));
    curl_setopt($multiCurl[$key], CURLOPT_RETURNTRANSFER,true);
    curl_multi_add_handle($mh, $multiCurl[$key]);
}
$index=null;
do {
  curl_multi_exec($mh,$index);
} while($index > 0);
// get content and remove handles
foreach($multiCurl as $k => $ch) {
  $result[$k] = curl_multi_getcontent($ch);
  curl_multi_remove_handle($mh, $ch);
}
// close
curl_multi_close($mh);
print_r($result);

在这里,我必须传递多个请求并获得结果,但是在这种情况下,没有结果。当我使用简单的curl时,结果即将到来。在这里,我的要求是减少响应时间。

2 个答案:

答案 0 :(得分:0)

您缺少“ curl_multi_select”的实现。我已经为您完成,但尚未测试。放手

$result = array();
// multi handle
$mh = curl_multi_init();
$multiCurl = array();
$idArr=[2,147,92];
foreach ($idArr as $key => $value) {
$fetchURL = "http://example.com/index.php/rest/V1/categories/".$value."/products/";
//echo $fetchURL.'<br>';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$fetch_url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_HEADER,array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
$multiCurl[$key] = $ch;
curl_multi_add_handle($mh, $ch);
}

$active =null;
do {
  $mrc = curl_multi_exec($mh,$active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);

while ($active && $mrc == CURLM_OK) {
  if (curl_multi_select($mh) != -1) {
    do {
        $mrc = curl_multi_exec($mh, $active);
    } while ($mrc == CURLM_CALL_MULTI_PERFORM);
  }
}   
foreach($multiCurl as $k => $ch) {
 $result[$k] = curl_multi_getcontent($ch);
 curl_multi_remove_handle($mh, $ch);
}
// close
curl_multi_close($mh);
print_r($result);

答案 1 :(得分:0)

我认为由于在curl选项集中设置了错误的变量而无法获得响应。foreach循环中的第三行应该是这样的

curl_setopt($multiCurl[$key], CURLOPT_URL,$fetchURL);

您还为标题写了错误的Syntex选项,将 CURLOPT_HEADER 替换为 CURLOPT_HTTPHEADER

这是完美运行的示例代码

$idArr=[20,18,21];

    $mh = curl_multi_init();
    $requests = array();

    $curl_objs_arr = [];
    foreach ($idArr as $key => $cat) {
        $fetchURL = "http:example.com/v2/products?category=".$cat;

        $requests[$key] = curl_init($fetchURL);

        curl_setopt($requests[$key], CURLOPT_URL,$fetchURL);
        curl_setopt($requests[$key], CURLOPT_CUSTOMREQUEST, "GET");
        curl_setopt($requests[$key], CURLOPT_HTTPHEADER,array("Content-Type: application/json","Authorization: Bearer " . json_decode($token)));
        curl_setopt($requests[$key], CURLOPT_RETURNTRANSFER,true);

        curl_multi_add_handle($mh, $requests[$key]);
    }

    $running = null;
    do {
       curl_multi_exec($mh, $running);
    } while($running > 0);

    foreach ($requests as $key => $request) {
        $result[$key] = curl_multi_getcontent($request);
        curl_multi_remove_handle($mh, $request);
    }
    curl_multi_close($mh);

    echo "<pre>";
    print_r($result);exit;

通过这种方式设置代码,可以解决您的问题。