icedev
icedev

Reputation: 215

Inconsistent Curl response from localhost to live site

Hi everyone I have a php file which returns a JSON which I constructed from a response from curl. My Problem is when I run it in locally(localhost) server the JSON response its length is 55 which is correct, it got all the events I needed. But when I tried it on our hosting site, the response is inconsistent, it is different from the localhost. For the first load I checked the length response and it returns 32 events out of 55, then I tried to reload it again then the response increased to 39 etc..

This is my localhost response. [https://prnt.sc/rll4w0]. This the file hosted on cpanel https://cdev.trilogycap.co/icedev/dynamic/civicore.php .This is the first response https://prnt.sc/rll2rc .And when I reload this page again https://prnt.sc/rll3fl

My php file function process is like this. I have a function which gets all the ID's of the event of this year and push it to the global array. I have removed the duplicate ID's there. So now I call a function for multicurl.

In which, for each ID I will process and call the API again and construct it. I then pushed it to my responseEvents variable array in global which then I encode to be able to make a valid JSON file. Is there a connection or wrong thing I did in my multi curl? Is their an execution limit to php multi curl? Or is the settimelimit(0) affect my code?

Here's my code

<?php
//Use this so that when we run the curl it wont timeout when dealing with getting data
set_time_limit(0);
$API_KEY  = "APIKEY";
$CURRENT_YEAR = date("Y");
$CURRENT_MONTH = date("m");
$CURRENT_DAY = date("d");
$events = getOpportunityYearly();//array of ID's removed duplicates [1,1,2,3,4,5 etc]
$noDuplicate =  array_values(array_unique($events));//remove $event ID duplciates
$responseEvents = [];//this is the array to be returned as json to use

//pass noDuplicate array which holds event ID's
multiCurl($noDuplicate);
print_r(json_encode($responseEvents, JSON_HEX_QUOT |  JSON_HEX_TAG | JSON_HEX_AMP |JSON_UNESCAPED_SLASHES ));

//returns an array of ID of events
function getOpportunityYearly(){
  $eventArr = [];//pass eventsID here
  $curl = curl_init();

  curl_setopt_array($curl, array(
      CURLOPT_URL => 'https://api.com/voc/api/v3/data/opportunities_dates?key='.$GLOBALS['API_KEY'].'&fields=["startDate","endDate","opportunities_id"]&where={"whereType":"AND","clauses":[{"fieldName":"startDate","operator":">=","operand":"'.$GLOBALS['CURRENT_YEAR'].'-'.$GLOBALS['CURRENT_MONTH'].'-'.$GLOBALS['CURRENT_DAY'].'"},{"fieldName":"endDate","operator":"<=","operand":"'.$GLOBALS['CURRENT_YEAR'].'-12-31"}]}',
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 0,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "GET",
  ));

  //convert response to obj
  $response = json_decode(curl_exec($curl));
  curl_close($curl);
  $eventsID = $response->records;
  //print_r($eventsID);

  //every opportunity object get its id and push to events array
  foreach($eventsID as $opportunity){
    array_push($eventArr,$opportunity->opportunities_id->value);
  }
  return $eventArr;
}

function multiCurl($eventArray){
    // array of curl handles
    $multiCurl = array();

    // data to be returned
    $result = array();

    // multi handle
    $mh = curl_multi_init();
    $index = null;

    foreach ($eventArray as $event) {
        //$event are the ID per each event
        $multiCurl[$event] = curl_init();
        curl_setopt_array($multiCurl[$event], array(
          CURLOPT_URL =>'https://api.com/voc/api/v3/data/opportunities/'.$event.'?key='.$GLOBALS['API_KEY'].'&fields=["opportunityName","typeOfWork","firstDateInOpportunity","lastDateInOpportunity","startTime","endTime","physicalDifficulty","minimumAge","location","state","city","county","campingAvailable","groupsAllowed","registrationFormID","cRQ_payment","paymentAmount","additionalInformation","photo","displayToPublic","latitude","longitude","summary","description","photo"]&where={"whereType":"AND","clauses":[{"fieldName":"displayToPublic","operator":"checked"}]}',
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_ENCODING => "",
          CURLOPT_MAXREDIRS => 10,
          CURLOPT_TIMEOUT => 0,
          CURLOPT_FOLLOWLOCATION => true,
          CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
          CURLOPT_CUSTOMREQUEST => "GET"
        ));
        curl_multi_add_handle($mh, $multiCurl[$event]);
    }

    do {
      curl_multi_exec($mh,$index);
    } while($index > 0);

      // get content and remove handles
      foreach($multiCurl as $key=>$value) {

        $records = json_decode(curl_multi_getcontent($value));//response of each request
        $record  = $records->records[0];

       if(strtolower($record->displayToPublic->displayValue) == 'yes'){
          $eve =  [ 
            "page_item_url"=> $record->opportunities_id->value,
              "data"=>[
                "typeOfWork"=>$record->typeOfWork->displayValue,
                "opportunityName"=> $record->opportunityName->displayValue,
                "firstDateInOpportunity"=> $record->firstDateInOpportunity->displayValue,
                "lastDateInOpportunity"=> property_exists($record, 'lastDateInOpportunity') ? $record->lastDateInOpportunity->displayValue : $record->firstDateInOpportunity->displayValue,
                "startTime"=>$record->startTime->displayValue,
                "endTime"=>$record->endTime->displayValue,
                "physicalDifficulty"=>$record->physicalDifficulty->displayValue,
                "minimumAge"=>$record->minimumAge->displayValue,
                "location"=>$record->location->displayValue,
                "state"=>$record->state->displayValue,
                "city"=>$record->city->displayValue,
                "county"=>$record->county->displayValue,
                "campingAvailable"=>$record->campingAvailable->displayValue,
                "groupsAllowed"=>$record->groupsAllowed->displayValue,
                "registrationFormID"=>$record->registrationFormID->displayValue,
                "cRQ_payment"=>$record->cRQ_payment->displayValue,
                "paymentAmount"=>$record->paymentAmount->displayValue,
                "photo"=>$record->photo->displayValue,
                "displayToPublic"=>$record->displayToPublic->displayValue,
                "latitude"=> property_exists($record, 'latitude') ? $record->latitude->displayValue : "0",
                "longitude"=> property_exists($record, 'longitude') ? $record->longitude->displayValue : "0",
                "photo"=>$record->photo->displayValue,
                "description"=> $record->description->displayValue,
                "additionalInformation"=> $record->additionalInformation->displayValue,
                "summary"=> $record->summary->displayValue
                ]
              ];
              array_push($GLOBALS["responseEvents"],$eve);
              curl_multi_remove_handle($mh, $value);
        }
      }//foreach
    curl_multi_close($mh);
}

?>


Upvotes: 3

Views: 423

Answers (1)

Will
Will

Reputation: 927

I think somehow your curl did not retreive all of the information when entering this foreach foreach($multiCurl as $key=>$value) That can explain why it is ok on local (fast response) and not on your distant server (network time, longer response time).

You should try this, which I get from documentation and should wait for all connections to properly end.

do {
    $status = curl_multi_exec($mh, $index);
    if ($index) {
        // Wait a short time for more activity
        curl_multi_select($mh);
    }
} while ($index && $status == CURLM_OK);

Upvotes: 1

Related Questions