Facebook Graph API requests take LONG? (file_get_contents) - php

I am using file_get_contents to retrieve stuff from the Facebook graph API (e.g: https://graph.facebook.com/me) and it takes like 5-10 seconds per request.
Any known issues on why this could be happening? When I ping the url from the browser it is really fast. Could it be a setting on my server?

It is possible. Use this cURL function :
function get_url($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$tmp = curl_exec($ch);
curl_close($ch);
return $tmp;
}
Does it work faster?

Related

bad url never ending request curl php

I have a curl set up like this:
function file_get_contents_curl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 400);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch,CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$data = curl_exec($ch);
return $data;
}
When I call it with the following:
$html = file_get_contents_curl("https://training.sap.com/service-id/module.php/core/as_login.php?AuthId=sf-sp&ReturnTo=%2Fsuccessfactors-community%2Fpermission-check");
You can see that link has an authentication, and I do not want to spend time on it. I just want that curl session to die after maybe 3 seconds or x seconds, or determine before hand if it is going to be a never ending loop on the curl for one reason such a SSL or password requirement etc.

Using gmaps api with key

I'm trying to call GMaps-API to geocode in PHP. When i use it without key it works, but if i use it with key on the URL it does not return any value. I want to pay per use in case i get over the 2.500 limit each day.
So this works:
$peticion="http://maps.googleapis.com/maps/api/geocode/json?address=STA.+MARIA+MAGDALENA%2C+16,28016,MADRID&sensor=false";
$res=file_get_contents($peticion);
and this does not work:
$peticion="https://maps.googleapis.com/maps/api/geocode/json?address=STA.+MARIA+MAGDALENA%2C+16,28016,MADRID&sensor=false&key=MY_API_KEY";
$res=file_get_contents($peticion);
I have an api key on google developper console. And the last link, on browser works ... so what am i doing wrong?
Thank you.
Solved. The problem is to download https. used the following:
function getSslPage($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}

cURL doesn't wait for a response

I am trying to fetch data from an API with cURL for PHP.
When I execute following script, cURL doesn't seem to wait for the request.
It immediately returns the empty field, which couldn't be populated.
function request($cityName)
{
$key = "abc";
$api = "https://...?api-key=$key&format=json&city=$cityName";
$api = urldecode(trim($api));
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $api);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_USERAGENT, 'abc.com');
$ret = curl_exec($curl);
$response = json_decode($ret);
var_dump($response);
curl_close($curl);
}
Try this, don't use urldecode
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($curl, CURLOPT_USERAGENT, 'abc.com');
$server_output = curl_exec($ch);
curl_close($ch);
print_r($server_output);
The OAuth extension uses curl to make the request. By default CURL will generally verify the SSL certificate to see if its valid an issued by an accepted CA. To do this, curl uses a bundled set of CA certificates.
You can either disable the SSL checks ($oauth->disableSSLChecks()). Or ensure that you have a current version of curl. More information on curl certification verification.
There are chances that the cURL cancels the request because of low speed connetction.
This happens if the average transfer rate drops below CURLOPT_LOW_SPEED_LIMIT bytes/second for CURLOPT_LOW_SPEED_TIME seconds.
Try the following:
curl_setopt($ch, CURLOPT_LOW_SPEED_LIMIT, 1); // cancel cURL if below 1 byte/second
curl_setopt($ch, CURLOPT_LOW_SPEED_TIME, 30); // Wait for 30 seconds
Change the values of the above options according to your requirements.

How do I call an API without reloading the page with PHP?

I am looking to call the Twitter API to grab tweets (successfully achieved tweets on load) but now I am looking to update the page automatically, allowing the tweets to automatically load without reload/user interaction.
I know this type of functionality is possible (monitter.com) does it, but what is the technology used to do so? Can I do it with PHP?
Thanks
As #suresh.g said, you can use AJAX. The simplest way: use jQuery.
Also, you can use an iframe that reloads every 10 seconds with the setInterval() javascript function. The user will not have a reload of his entire page, but the twitter iframe.
Another type of technology is COMET or PUSH technology, but I don't think you need it right now, but it's good to know about it ;)
use curl
function curl_grab_page($url,$data,$secure="false",$ref_url="",$login = "false",$proxy = "null",$proxystatus = "false")
{
if($login == 'true') {
$fp = fopen("cookie.txt", "w");
fclose($fp);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
if ($proxystatus == 'true') {
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, TRUE);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
}
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
if($secure=='true')
{
curl_setopt($ch, CURLOPT_SSLVERSION,3);
}
curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Expect:' ) );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $ref_url);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
ob_start();
return curl_exec ($ch); // execute the curl command
curl_getinfo($ch);
ob_end_clean();
curl_close ($ch);
unset($ch);
}
just call this function the way you want the data you want to set it can do every thing for you just dont forget to setup curl in you php.ini
thanks

How to limit cURL connetions

i have written a small script to connect via cURL to my API, however, i need to know how i can limit incoming cURL connections to prevent spam.
How can this be done?
<?php
function shorten_url($urltoshorten) {
$url = 'http://nn.pe/api.php?url='.$urltoshorten;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
// what to post
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
?>
You can use: curl_set_opt(CURLOPT_MAXCONNECTS, 10); for limiting connections you made by curl. Or you can create a table and keep record of IPs requesting for connections.
Hope this helps.

Categories