cURL PHP multiple requests for a dynamic URL (Looping Unknown Times) - php

I'm new to cURL & I'm trying to retrieve data from a dynamic URL. I've successfully retrieved data from 1 page only, but what I want is retrieving data from all pages. The problem here is that pages are variable; I don't know how many time the code should loop to loop over all pages. i.e. the number of pages varies from one case to another, and a good program is the one that works in as many cases as possible. Consequently, putting the links in an array and looping over them isn't the right solution for this.
Here's a quick explanation of the URL I'm trying to retrieve data from:
https://link-search.api.cj.com/v2/link-search?website-id=[Your-ID]&link-type=banner
&advertiser-ids=1513033&records-per-page=100&page-number=' . $num
Did you notice the last variable $num? That should represent the number of the page from where data will be retrieved from. In some cases, it could be only 1, and in some other cases, it could be 10, 12 or 15 (it varies) depending on the parameters I choose, whether I want to see everything or filter some information.
Now here is the problem. How to dynamically increment that number as long as the request returns data? and if not, cURL should stop running?
Here's the code:
<?php
$num = 1;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "XGET");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($curl, CURLOPT_ENCODING, "UTF-8");
curl_setopt($curl, CURLOPT_URL, 'https://link-search.api.cj.com/v2/link-search?website-id=[Your-ID]&link-type=banner&advertiser-ids=1513033&records-per-page=100&page-number=' . $num);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_COOKIE,true);
$result = curl_exec($curl);
$xml = simplexml_load_string($result) or die("Error: Cannot create object");
if (curl_errno($curl)) {
echo 'Error:' . curl_error($curl);
}
curl_close($curl);
?>

You can define your curl request inside the function and call that function like this:
<?php
$GLOBALS['num'] = 1;
function curlRequest()
{
// HERE DEFINE YOUR CURL REQUEST
// https://yourUrl.com?$GLOBALS['num']
if (curl_errno($curl)) {
echo 'Error:' . curl_error($curl);
// Exit from function in case there is no output
return;
} else {
$GLOBALS['num']++;
// Call the function to fetch NEXT page
curlRequest();
}
}
// Call the function for first time
curlRequest();
Assuming that curl will fire error if page number not exist or you can implement if condition from the result you are getting. Hope you will get some idea from this.
The main part here is if & else. You should implement if condition on the output you are getting and if page not exist then output will be different so you can simply return from the function in this case.

Related

Get all the contacts from HubSpot list using API

I am using the contact API but it returns maximum 250 contacts only. I used 'vidOffset' parameter for next page but no luck.
Note: I want to export all the contacts from the Hubspot List to my local database using API
Here is my code with php curl:
function callAPI($method, $url, $data){
$curl = curl_init();
$url = $url.'&property=firstname&property=email&count=5&vidOffset=2';
switch ($method){
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}
// OPTIONS:
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
// EXECUTE:
$result = curl_exec($curl);
if(!$result){die("Connection Failure");}
curl_close($curl);
return $result;
}
// call the function
callAPI('GET', 'https://api.hubapi.com/contacts/v1/lists/11/contacts/all?hapikey=[API key]', false);
Is there anything wrong I am doing? or if there is a better way to get all contact using php/wordpress then please share your experience.
There are a few things to look for here when you make your calls to this API.
Is there a "true" value in the "has-more" field. If so, there are more contacts that can be pulled.
The value of the "vid-offset" field that is returned in your calls.
For the "has-more", this boolean specifies whether or not there are more contacts that you can pull via pagination. For the "vid-offset", this is an integer generated by the API, it does not take a simple sequential integer.
Additionally, you're only grabbing 5 records at a time, you may as well just do the maximum, since it's only 100. This will limit the number of calls you need to make.
Lastly, you may just want to add these to a file, which you can then use for whatever you want, i.e. adding to database, download etc.
So, my suggestion is to amend your initial function to check for the "has-more" value of "true", if it is true send the "vid-offset" value to a new function that makes another call. In that function, you can continue to check for those values, and run your function as many times as it takes until that "has-more" value turns up false.
// the rest of your function is above
// Decode the result so you can traverse the data
$contacts = json_decode($result);
// Store 'has-more' value
$has_more = $contacts->has-more;
// Check if there are more records
if($has_more) {
// Get the offset number provided by API
$offset = $contacts->vid-offset;
// Get more records
getMore($offset);
} else {
// Complete calls and do something else...
}
}
function getMore($offset) {
// Make cURL call with your your offset value
$url = $url.'&property=firstname&property=email&count=100&vidOffset=' . $offset;
$contacts = json_decode($result);
$has_more = $contacts->has-more;
if($has_more) {
$offset = $contacts->vid-offset;
getMore($offset);
} else {
// Complete calls and do something else...
}
}
The documentation that they provide is actually quite clear, so I would read it over a bit as well.

Getting Instagram's followers data using Curl

I'm trying to fetch the number of followers of an instagram account through web scraping and curl. Using their API may be easier but i want to know why this won't work, because in many case i got the data through HTML.
static $url='https://www.instagram.com/cats_of_instagram/';
function getUrlContent($url){
try {
$curl_connection = curl_init($url);
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
//Data are stored in $data
$data = (curl_exec($curl_connection));
$position = strpos($data,"<span data-reactid=\".0.1.0.0:0.1.3.1.0.2\"> followers</span>");
print_r($position);
curl_close($curl_connection);
} catch(Exception $e) {
return $e->getMessage();
}
}
Problem is the function strpos does not return position.
$position = strpos($data,"<span data-reactid=\".0.1.0.0:0.1.3.1.0.2\"> followers</span>");
You can't do that.
The element you're looking for is rendered by javascript, after the page has loaded.
curl doesn't wait for scripts to run (nor does it run any). It just returns the html.
You can easily verify this by printing $data. Or by looking at the page's source.
To "see" the element you're looking for, you need to use the DOM inspector.

passing variable to another page when it is called using curl_setopt

I am usig curl_setopt to eval another file from a different server, the link is something like this "http://somewhere.com/index.php?vars=hello"
now in somewhere.com/index.php i need to get the value of vars that was passed using curl, but so far i cant get any values at all.
here is my sample code for your reference this is from the file calling somewhere.com:
$d = "http://somewhere.com/index.php?vars=hello";
$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch1, CURLOPT_URL, $d);
curl_setopt($ch1, CURLOPT_FOLLOWLOCATION, TRUE);
$data1 = curl_exec($ch1);
eval($data1);
curl_close($ch1);
in somewhere.com/index.php i already did print_r($_GET); to view any passed values to the file but it returned nothing.
What is happening in the index.php file with the variable "hello"? When you do a curl post to another page that other page typically will echo out a response and that response is what you evaluate. Can you post your code from index.php so we can see what you are trying to do?
Edit: Also you can use chrome inspector, fiddler or some other network monitor to see if the http request actually is being fired off and to check that you are actually getting a 200 response back.
Edit: I don't know what you are using eval either, just echo the response. If you have an output buffer issue then start buffering before you echo the response like:
ob_start():
echo $data1;
ob_end_clean();
Also if you want to see if you are getting any errors just do this:
if(curl_exec($ch1) === false)
{
echo 'Curl error: ' . curl_error($ch1);
}
else
{
echo $data1;
}
// Close handle
curl_close($ch1);
can't you just use $_GET[]?
i.e.
$variables = $_GET['vars'];

curl_exec not working inside a loop

I have a class with the function below. When I call that function directly it works, but if I call that function inside the loop I got an error: Cannot connect to host. Can somebody help me? :) Thanks in advance.
public function exportProfile($xml) {
$_myRequest = curl_init("https://www.example.com/postProfile");
curl_setopt($_myRequest, CURLOPT_POST, 1);
curl_setopt($_myRequest, CURLOPT_USERPWD, "testUser:testPassword");
curl_setopt($_myRequest, CURLOPT_HTTPHEADER, Array("Content-Type: application/xml"));
curl_setopt($_myRequest, CURLOPT_POSTFIELDS,$xml );
curl_setopt($_myRequest, CURLOPT_RETURNTRANSFER, 1);
// do request, the response text is available in $_response
$_response = curl_exec($_myRequest);
$err = curl_error($_myRequest) ;
echo $err;
$_statusCode = curl_getinfo($_myRequest, CURLINFO_HTTP_CODE);
// close cURL resource, and free up system resources
curl_close($_myRequest);
return simplexml_load_string($_response);
}
I can't reproduce that problem with a simple example. Your example code seems to have a syntax error in CURLOPT_USERPWD, but I assume that is not related.
It sounds quite likely that there could there be rate limiting on the site you are querying, i.e. when you hit them a few times in a row they lock you out.
Perhaps you could debug by trying to run the query multiple times outside of the loop, i.e. calling it 1/2/3 times manually.

Why are the Twitter api calls so slow?

When I execute the following code it takes between 10-12 seconds to respond.
Is the problem with Twitter or with our server?
I really need to know as this is part of the code to display tweets on our website and a 12 second load time is just not acceptable!
function get_latest_tweets($username)
{
print "<font color=red>**". time()."**</font><br>";
$path = 'http://api.twitter.com/1/statuses/user_timeline/' . $username.'.json?include_rts=true&count=2';
$jason = file_get_contents($path);
print "<font color=red>**". time()."**</font><br>";
}
Thanks
When you put the URL into your browser (http://api.twitter.com/1/statuses/user_timeline/username.json?include_rts=true&count=2) how long does it take for the page to appear? If it's quick then you need to start the search at your server.
use curl instead of file_get_contents() to request, so that response will be compressed. Here is the curl function which iam using.
function curl_file_get_contents($url)
{
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,$url); //The URL to fetch. This can also be set when initializing a session with curl_init().
curl_setopt($curl,CURLOPT_RETURNTRANSFER,TRUE); //TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.
curl_setopt($curl,CURLOPT_ENCODING , "gzip");
curl_setopt($curl, CURLOPT_FAILONERROR, TRUE); //To fail silently if the HTTP code returned is greater than or equal to 400.
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
$contents = curl_exec($curl);
curl_close($curl);
return $contents;
}

Categories