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);
}
?>
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);
Related
I am working with an API at the moment that will only return 200 results at a time, so I am trying to run some code that works out if there is more data to fetch based on whether or not the results have a offsetCursor param in them as this tells me that that there are more results to get, this offsetCursor is then sent a param in the next request, the next set of results come back and if there is an offsetCursor param then we make another request.
What I am wanting to do is push the results of each request into a an array, here is my attempt,
function get_cars($url, $token)
{
$cars = [];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Content-Type: application/x-www-form-urlencoded",
"Authorization: Bearer " . $token
)
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if($err) {
return false;
} else {
$results = json_decode($response, TRUE);
//die(print_r($results));
$cars[] = $results['_embedded']['results'];
if(isset($results['cursorOffset']))
{
//die($url.'&cursor_offset='.$results['cursorOffset']);
get_cars('https://abcdefg.co.uk/service/search1/advert?size=5&cursor_offset='.$results['cursorOffset'], $token);
//array_push($cars, $results['_embedded']['results']);
}
}
die(print_r($cars));
}
I assume I am doing the polling of the api correct in so mush as that if there is a cursor offet then I just call the function from within itself? But I am struggling to create an array from the results that isnt just an array within and array like this,
[
[result from call],
[resul from call 2]
]
what I really want is result from call1 right through to call n be all within the same sequential array.
using a do+while loop, you'll have only 1 instance of cars variable, that would work.
Since you're using recursion, when you call get_cars inside get_cars, you have 2 instances of cars variable, one per get_cars call.
IMHO, using a loop is better in your case.
But if you still want to use recursion, you should use the result of get_cars call, something like this:
if(isset($results['cursorOffset']))
{
//die($url.'&cursor_offset='.$results['cursorOffset']);
$newcars = get_cars('https://abcdefg.co.uk/service/search1/advert?size=5&cursor_offset='.$results['cursorOffset'], $token);
$cars = array_merge($cars, $newcars);
//array_push($cars, $results['_embedded']['results']);
}
(and get_cars should return $cars, instead of printing it with print_r)
Edit: here is an example of, untested, code with a while loop (no need for do+while here)
<?php
function get_cars($baseUrl, $token)
{
$cars = [];
// set default url to call (1st call)
$url = $baseUrl;
while (!empty($url))
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Content-Type: application/x-www-form-urlencoded",
"Authorization: Bearer " . $token
)
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if($err)
{
// it was "return false" in your code
// what if it's the 3rd call that fails ?
// - "return $cars" will return cars from call 1 and 2 (which are OK)
// - "return false" will return no car (but call 1 and 2 were OK !!)
return $cars;
}
$results = json_decode($response, TRUE);
$cars[] = $results['_embedded']['results'];
if(isset($results['cursorOffset']))
{
// next call will be using this url
$url = $baseUrl . '&cursor_offset='.$results['cursorOffset'];
// DONT DO THE FOLLOWING (concatenating with $url, $url = $url . 'xxx')
// you will end up with url like 'http://example.com/path/to/service?cursor_offset=xxx&cursor_offset==yyy&cursor_offset==zzz'
// $url = $url . '&cursor_offset='.$results['cursorOffset'];
}
else
{
$url = null;
}
}
return $cars;
}
The following PHP code using foreach does not seem to work. I believe it has to do with "<a href='/$value/access'>".
I've shared the entire codebase.
Does anyone know what is wrong with my statement?
// include/functions.php
<?php
// Defining the basic cURL function
function curl($url) {
// Assigning cURL options to an array
$options = Array(
CURLOPT_RETURNTRANSFER => TRUE, // Setting cURL's option to return the webpage data
CURLOPT_FOLLOWLOCATION => TRUE, // Setting cURL to follow 'location' HTTP headers
CURLOPT_AUTOREFERER => TRUE, // Automatically set the referer where following 'location' HTTP headers
CURLOPT_CONNECTTIMEOUT => 120, // Setting the amount of time (in seconds) before the request times out
CURLOPT_TIMEOUT => 120, // Setting the maximum amount of time for cURL to execute queries
CURLOPT_MAXREDIRS => 10, // Setting the maximum number of redirections to follow
CURLOPT_USERAGENT => "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1a2pre) Gecko/2008073000 Shredder/3.0a2pre ThunderBrowse/3.2.1.8", // Setting the useragent
CURLOPT_URL => $url, // Setting cURL's URL option with the $url variable passed into the function
);
$ch = curl_init(); // Initialising cURL
curl_setopt_array($ch, $options); // Setting cURL's options using the previously assigned array data in $options
$data = curl_exec($ch); // Executing the cURL request and assigning the returned data to the $data variable
curl_close($ch); // Closing cURL
return $data; // Returning the data from the function
}
// Defining the basic scraping function
function scrape_between($data, $start, $end){
$data = stristr($data, $start); // Stripping all data from before $start
$data = substr($data, strlen($start)); // Stripping $start
$stop = stripos($data, $end); // Getting the position of the $end of the data to scrape
$data = substr($data, 0, $stop); // Stripping all data from after and including the $end of the data to scrape
return $data; // Returning the scraped data from the function
}
?>
// code.php
<?php
//include functions
include_once("include/functions.php");
// Set URL
$url = "https://www.instituteforsupplymanagement.org/ismreport/mfgrob.cfm";
$source = curl($url);
// Collect dataset
$arr = array("PMI","New Orders");
foreach ($arr as $value) {
$data = scrape_between($source,"<strong>$$value","</tr>");
print_r($data);
}
?>
within the string the second $ is being treated as a string, so you end up with strings like '$PMI'. why not just assign your variable variable to a temp variable:
$start = $$value;
$data = scrape_between($source,"<strong>$start","</tr>");
also I don't know how you plan to use "New Orders" as a variable name what with the space and all.
maybe you're trying to use "PMI" and "New Orders" in the string, in which case just drop the extra $
I'm creating a function to untilize NameCheap's API for registering domain names. The registration process worked out smoothly, now I'm looking to set the proper DNS Hosts.
When I create a pure POST request with something like POSTMAN this works fine and returns the expected XML response. However when I try to pass the data through PHP's CURL functions it breaks. I've narrowed the problem the the '#' symbol that needs to be passed to the DNS Host. If i put anything else there the request goes through. I've tried to url_encode the symbol but the API does not accept that.
Any suggestions?
public function setDNSHost($name, $server){
list($domain,$tld) = explode('.',$name,2);
$request = $this->request_URL;
$curl = curl_init();
$args['ApiUser'] = $this->API_User;
$args['ApiKey'] = $this->API_Key;
$args['UserName'] = $this->API_User;
$args['Command'] = 'namecheap.domains.dns.setHosts';
$args['ClientIP'] = $this->Client_IP;
$args['SLD'] = $domain;
$args['TLD'] = $tld;
$args['HostName1'] = utf8_encode('#');
$args['RecordType1'] = 'A';
$args['Address1'] = $server;
$args['HostName2'] = 'www';
$args['RecordType2'] = 'CNAME';
$args['Address2'] = $name;
$args['HostName3'] = '*';
$args['RecordType3'] = 'CNAME';
$args['Address3'] = $name;
curl_setopt_array($curl, array(
CURLOPT_URL => $request,
CURLOPT_USERAGENT => 'API',
// CURLOPT_FAILONERROR => 1,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $args,
CURLOPT_TIMEOUT => 15
));
$response = curl_exec($curl);
curl_close($curl);
// $oXML = new SimpleXMLElement($response);
return $response;
}
Some caracters are not allowed to be in the string. To avoid such problems you could use http_build_query on your data before you use the curl function.
I am trying to send cURL request to a remote API server with this code:
$ch = curl_init();
$options = array(CURLOPT_URL => 'http://minecms.info/update/index.php',
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_POSTFIELDS => $data,
CURLOPT_HTTPHEADER => array('Content-type: application/json'),
CURLOPT_SSL_VERIFYPEER => false
);
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
But I don't want users accessing the updates page on their browsers so I set a content type header on the request. The problem is that I don't know how to detect this content type on the remote server. Basically what I want is to check whether the client request has a content type: application/json set if yes it executes the rest of the code if not it just does exit;.
Thank you to anyone who would help in advance.
You can try using getallheaders() and check whether the Content-Type is in place.
Give a look at the man http://www.php.net/manual/it/function.getallheaders.php for insights
---- EDIT INSIGHTS ----
And what about this one? (Which I'm currently using)
public function getAllHeaders()
{
if(function_exists('getallheaders'))
{
return getallheaders();
}
$headers = array();
foreach ($this->parameters as $key => $value)
{
if (substr($key, 0, 5) == 'HTTP_')
{
$headers[str_replace(" ", "-", ucwords(strtolower(str_replace("_", " ", substr($key, 5)))))] = $value;
}
if ($key == "CONTENT_TYPE")
{
$headers["Content-Type"] = $value;
}
}
return $headers;
}
I am facing one unusual behavior of curl. For a given page, I some times get HTTP response code as 200 and sometimes I get 0 as HTTP response code. I am not able to understand whether this page is valid or not. If you try the given code, please try it for at least 5-10 times so that you can see the difference.
function print_info()
{
$url = 'bart.no';
$arr = array(
'bart.no',
'bolandirekt.nu',
'ekompassen.com',
'ekompassen.nu',
);
foreach ($arr as $url)
{
echo "<br/>URL: " . $url;
$temp = str_replace(array("www.", "http://", "https://"), "", strtolower($url));
// From this array it will be decided which is to prepend
$pre_array = array("", "www.", "https://", "http://", "https://www.", "http://www.");
$status_code = array();
// For each Value Status will be stored
foreach ($pre_array as $pre)
{
$options = array(
CURLOPT_RETURNTRANSFER => TRUE, // return web page
CURLOPT_HEADER => TRUE, // don't return headers
CURLOPT_FOLLOWLOCATION => FALSE, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "spider", // who am i
CURLOPT_AUTOREFERER => FALSE, // set referer on redirect
CURLOPT_SSL_VERIFYHOST => FALSE, //ssl verify host
CURLOPT_SSL_VERIFYPEER => FALSE, //ssl verify peer
CURLOPT_NOBODY => FALSE,
CURLOPT_CONNECTTIMEOUT => 20, // timeout on connect
CURLOPT_TIMEOUT => 20, // timeout on response
);
// Initializing Curl
$ch = curl_init($pre . $temp);
// Set Curl Options
curl_setopt_array($ch, $options);
// Execute Curl
$content = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo "<pre/>";
if ($code == 200)
{
print_r(curl_getinfo($ch));
break;
}
curl_close($ch);
}
}
}
So my final doubt is : Why I am getting response code 200 for the pages which are not existing Or not opening in browser ? Also, why sometimes I get response code 0 and sometimes response code 200 for the same page even if I keep time interval between requests ?
The CURL request did not complete, thus there's no response code.
The reason for this may be an invalid host name (can't resolve), malformed URL, timeout, etc.
You should be able to get the CURL error code as in CodeCaster's comment and curl_error / curl_errno docs.
Once the CURL request completed properly, then a response code (from the server) should be available and meaningful.