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;
}
Related
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);
I am trying to pull data from API for active collab, but in the body to the task exist tags HTML, causing a mess in the coding, anybody know what I can do?
My code to push API:
try {
function listTasks() {
$ch = curl_init();
$token = 'token';
curl_setopt_array($ch, [
CURLOPT_URL => 'https://collab.cadastra.com.br/api/v1/projects/projectnumber/tasks/',
CURLOPT_HTTPHEADER => [
'X-Angie-AuthApiToken: ' . $token,
'Content-Type: application/json',
'x-li-format: json'
],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 30,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_PROTOCOLS => CURLPROTO_HTTPS
]);
$result = curl_exec($ch);
// $tasks = json_decode($result, true);
// for ($i = 0; $i < count($tasks); $i++) {
// if ($tasks[$i]["task_list_id"] == 55979) {
// $tasks_name[$i] = $tasks[$i]["name"];
// }
// }
print_r(filter_var($result, FILTER_SANITIZE_MAGIC_QUOTES));
curl_close($ch);
// return $resultado;
}
listTasks();
} catch (Error $e) {
print_r($e->getMessage());
}
// print_r($_POST['email']));
This return:
you can use one of PHP function for clean view:
strip_tags (https://www.php.net/manual/en/function.strip-tags)
htmlentities (https://www.php.net/manual/en/function.htmlentities)
for view the tasks, otherways you must look at the source of request, cause browser is rendering html. if you will have any errors for this, you can the function use for task body.
I hope it help to you. :)
My Controller.
public function showMonthlyReport($site_id, $report_id){
$reports = Report::where('report_id', $report_id)->firstOrFail();
$uptime = ???
return view('records', compact('site_id', 'report_id', 'reports', 'uptime'));
}
And my UptimeRobot.php reference https://uptimerobot.com/api getMonitors()method
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.uptimerobot.com/v2/getMonitors",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "Your Api Key",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: application/x-www-form-urlencoded"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
$data = json_decode($response);
$custom_uptime = ($data->monitors[0]->custom_uptime_ratio);
$uptime = explode("-",$custom_uptime);
}
?>
ApiCommand.php
public function handle()
{
include(app_path() . '/Includes/DeepCrawl.php');
include(app_path() . '/Includes/Uptime.php');
include(app_path() . '/Includes/HelloAnalytics.php');
$stringData = ApiCommand::drawGraph($uptime, $dates, $users, $otherResultsRows, $array_issues, $array_pages_breakdown, $array_uncrawled_url, $array_non_200_pages, $array_orphaned_pages, $array_non_indexable_pages, $array_crawl_source_gap, $array_https_http);
Storage::disk('local')->put('hello.txt', $stringData);
}
Currently building a laravel web application.
I am just wondering how can i able to gather data from uptimerobot. I'm going to use my controller so I can pass it to my view but I don't know how. I have code below with the curl php type above. Really confused what am I doing new programmer here. Can someone explain if I'm at the right path or is it possible to do in controller. Thanks in advance.
I can suggest slightly different solution:
Extract your curl code in a separate console command and run this command each minute (for example, as a cron job).
The result of the command save to database/file/memory.
In your showMonthlyReport() refer to existing result.
Benefits:
In this way you would not have to wait for your curl result on each showMonthlyReport(). All code will run asynchronously
All errors processing will be in one place
Command is testable
require_once('db_lib.php');
$oDB = new db;
$result = $oDB->select('select * from tweet_urls');
while ($row = mysqli_fetch_row($result)) {
//echo $row['1'].'</br>';
echo get_follow_url($row['1']);
}
function get_follow_url($url) {
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_HEADER => false,
CURLOPT_NOBODY => true,
CURLOPT_FOLLOWLOCATION => true,
));
curl_exec($ch);
$follow_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
curl_close($ch);
return $follow_url;
}
I extract the tweets urls from twitter and I want to change the short urls into its original long urls.
What is wrong in my code. I call the function get_follow_url($url) inside the while loop. I think I do some mistakes in calling array get_follow_url($row['1']) inside the call function .
You need to use the field position which will be $row[1] without quotes. If there is a field named 1 you will need to use the mysqli_fetch_assoc() or mysqli_fetch_array() methods
I'm trying to use curl instead of the http request 2 pear module in PHP to query the plivo api. They have an existing library for easily making calls to their API but it uses a pear module called http request2. I don't really know how to install a pear module on a server so I thought of just rewriting some parts of their library to just use curl.
Here's the part of their code that I specifically want to modify:
function __construct($auth_id, $auth_token, $url="https://api.plivo.com", $version="v1") {
if ((!isset($auth_id)) || (!$auth_id)) {
throw new PlivoError("no auth_id");
}
if ((!isset($auth_token)) || (!$auth_token)) {
throw new PlivoError("no auth_token");
}
$this->version = $version;
$this->api = $url."/".$this->version."/Account/".$auth_id;
$this->auth_id = $auth_id;
$this->auth_token = $auth_token;
}
private function request($method, $path, $params=array()) {
$url = $this->api.rtrim($path, '/').'/';
if (!strcmp($method, "POST")) {
$req = new HTTP_Request2($url, HTTP_Request2::METHOD_POST);
$req->setHeader('Content-type: application/json');
if ($params) {
$req->setBody(json_encode($params));
}
} else if (!strcmp($method, "GET")) {
$req = new HTTP_Request2($url, HTTP_Request2::METHOD_GET);
$url = $req->getUrl();
$url->setQueryVariables($params);
} else if (!strcmp($method, "DELETE")) {
$req = new HTTP_Request2($url, HTTP_Request2::METHOD_DELETE);
$url = $req->getUrl();
$url->setQueryVariables($params);
}
$req->setAdapter('curl');
$req->setConfig(array(
'timeout' => 30,
'ssl_verify_peer' => FALSE,
));
$req->setAuth($this->auth_id, $this->auth_token, HTTP_Request2::AUTH_BASIC);
$req->setHeader(array(
'Connection' => 'close',
'User-Agent' => 'PHPPlivo',
));
$r = $req->send();
$status = $r->getStatus();
$body = $r->getbody();
$response = json_decode($body, true);
return array("status" => $status, "response" => $response);
}
public function get_account($params=array()) {
return $this->request('GET', '', $params);
}
And here's the code that I have so far:
<?php
$curl = curl_init();
$curl_options = array(
CURLOPT_URL => 'https://api.plivo.com/v1/Account/',
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_USERPWD => 'auth_id:auth_token',
CURLOPT_HTTPHEADER => array("Connection: close", "User-Agent: PHPPlivo"),
CURLOPT_TIMEOUT => 30
);
curl_setopt_array($curl, $curl_options);
$response = curl_exec($curl);
curl_close($curl);
?>
I don't really know what's going on behind the scenes but this specific code is telling me that its using basic authentication using the values for the auth id and auth token:
$req->setAuth($this->auth_id, $this->auth_token, HTTP_Request2::AUTH_BASIC);
So I also set it using curl:
CURLOPT_USERPWD => 'auth_id:auth_token',
I'm pretty much stuck. All I get as a respose is the following:
{
"error": "not found"
}
It doesn't really make much sense into what I have missed or done wrong. Please help. Thank you in advance!
Below are the things you need to handle to sync your new code with old one:
If you are using GET method
CURLOPT_URL => 'https://api.plivo.com/v1/Account/'.http_build_query($params),
CURLOPT_HTTPHEADER => array("User-Agent: PHPPlivo"),
If you are using POST method
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query($params),
CURLOPT_HTTPHEADER => array("Content-type: application/json", "User-Agent: PHPPlivo"),
Yea... The PEAR dependency is definitely overkill for Plivo's wrapper. So that was one of the first modifications I made to the code.
Check out:
https://github.com/ashbeats/Plivo-Curl-Based-Wrapper/
Only difference is the RestAPI::request() method.