0

I am fetching the data using the curl in php but as many data are present it returning the 0 output. I am providing my code below.

 $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);

Here I have to pass the multiple request and get the result but in this case no result is coming. While I am using simple curl the result is coming. Here my requirement is to reduce the response time.

halfer
  • 19,824
  • 17
  • 99
  • 186
satya
  • 3,508
  • 11
  • 50
  • 130
  • Why are json DEcoding the token? Decoding creates an array. I dont think that's what you need. – delboy1978uk Aug 31 '18 at 11:51
  • why is `$multiCurl` an array? And where is `$i` coming from? Right now you don't seem to be changing $i (or declaring it even) at all. So the array part of it makes no sense. It keeps writing to the same element now. – Dirk Scholten Aug 31 '18 at 11:53
  • @DirkScholten : Sorry I made the changes still same error. – satya Aug 31 '18 at 12:03
  • @raina77ow : I did as per you but same result(`Array ( [0] => [1] => [2] => )`) is coming. There is no error message printing. – satya Aug 31 '18 at 12:07
  • @delboy1978uk : token need to pass and while I am using the simple `curl_init` its working fine. – satya Aug 31 '18 at 12:08
  • can you `var_dump($token)` for us? Also, try using https://secure.php.net/manual/en/function.curl-error.php to get more information – delboy1978uk Aug 31 '18 at 12:11
  • @delboy1978uk ; This is `"749nkj225o0j4en38dxvb7hx3n34c7gy"` token value and its need while calling the `magento2` `end point API`. – satya Aug 31 '18 at 12:14
  • @raina77ow : `array(3) { [0]=> string(0) "" [1]=> string(0) "" [2]=> string(0) "" }` is the output for `var_dump($result)` – satya Aug 31 '18 at 12:15
  • So it's an empty string, great. Ok, have you tried to set up the proxy, as [in this question](https://stackoverflow.com/questions/41263081/curl-multi-getcontent-returns-empty-string)? Also, does anything useful appear with `curl_setopt($ch, CURLOPT_VERBOSE, 1)`? – raina77ow Aug 31 '18 at 12:16
  • @raina77ow : same issue. – satya Aug 31 '18 at 13:03

2 Answers2

0

You are missing the implementation of "curl_multi_select". I have done it for you but not tested. Give it a go

$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);
Rinsad Ahmed
  • 1,877
  • 1
  • 10
  • 28
0

I think you can't get response due to set wrong variable in curl option set.Your third line in foreach loop should be like that

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

also you write wrong syntex for headers option replace CURLOPT_HEADER by CURLOPT_HTTPHEADER

here is sample code that works perfectly

$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;

set your code by this way, that's solve your problem.

Harry baldaniya
  • 167
  • 1
  • 11