gzdecode and file_get_contents in php vs curl - php

$url = 'https://api.stackexchange.com/2.2/answers/'.$id.'?order=desc&sort=activity&site=stackoverflow&filter=!-*f(6t*ZdXeu&key=MY_KEY';
gzdecode(file_get_contents ($url)) ;
this caused me problems today when I played with stackoverflow APIs

alternative approach - just use CURL, it does decompression automatically
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 400);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); // Follow redirects, need this if the url changes
curl_setopt($curl, CURLOPT_MAXREDIRS, 5); //if http server gives redirection responce
curl_setopt($curl, CURLOPT_ENCODING, "gzip"); // the page encoding
$data = curl_exec($curl); // data already decompressed
curl_close($curl);

Related

Converge API - check if API is available

I am using Converge API to transfer funds from Credit Cards to my merchant account in my system.
This is the API URL that I am using:
https://api.convergepay.com/VirtualMerchant/process.do
Sometimes I am having connectivity issues to the API and as a result, I am getting blank screens or similar errors.
Is there a way I can check if the API is available before I do the CCSALE transaction? This is a simple code I setup to try and achieve this task.
$url = 'https://api.convergepay.com/VirtualMerchant/process.do';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$result = curl_exec($ch);
//$result will be false if the CURL request fails.
curl_close($ch);
Try getting the error info and then redirect the user. Use the code below to get a detailed response from CURL
$url = 'https://api.convergepay.com/VirtualMerchant/process.do';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_VERBOSE, true);
//Tell cURL that it should only spend 10 seconds
//trying to connect to the URL.
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
//A given cURL operation should only take
//30 seconds max.
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
$httpCode = curl_getinfo($curl , CURLINFO_HTTP_CODE);
$response = curl_exec($curl);
if ($response === false)
$response = curl_error($curl);
echo stripslashes($response);
curl_close($curl);

php reuse curl_setopt's from previous request?

Is it possible to set multiple curl_setopt only once and re-use them in future curl exec's as long as the curl handle is not closed ? (Especially Useragent and the Cookie)
<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, TRUE);
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 Gecko/20100101 Firefox/49.0");
curl_setopt($curl, CURLOPT_COOKIE, "PHPSESSID=".session_id());
curl_setopt($curl, CURLOPT_URL, "https://foo.bar/action/");
$ret = curl_exec($curl);
## DO SOME STUFF ##
curl_setopt($curl, CURLOPT_URL, "https://foo.bar/anotherAction/"); // Set only new URL ..
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // ... and the POST data.
$ret2 = curl_exec($curl);
curl_close($curl);
?>
Solution is as simple as the question .. just do it the way I did in the question. As long as the cURL session is not closed by curl_close();, you can query as many requests as you like with the headers of your choice only set once after curl_init();.

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.

Running two cURL scripts in same function seems to be blocking second action

I have a script wherein I would like to send off two HTTPS calls to a master database via cURL with similar variables. I have tested each script individually to be working fine, but when I combine the two the second one is blocked. Here is what it looks like when they are combined:
$idno=’123’;
$curl = curl_init();
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_URL, 'https://mysite.com/integration.php?type=CANCEL&idnumber='.$idno.'');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$Response = curl_exec($curl);
return $Response;
curl_close($curl);
$curl2 = curl_init();
curl_setopt($curl2, CURLOPT_VERBOSE, 1);
curl_setopt($curl2, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl2, CURLOPT_TIMEOUT, 30);
curl_setopt($curl2, CURLOPT_URL, 'https://mysite.com/integration2.php?customer_id=CUSOTMER&idnumber='.$idno.’&type=CANCEL');
curl_setopt($curl2, CURLOPT_RETURNTRANSFER, 1);
$Response2 = curl_exec($curl2);
return $Response2;
curl_close($curl2);
I have tried a handful of variations to open and close the cURL calls, but no luck. Anything standing out here as off?
First of all, you have syntax error on your code. Please fix those. For example $idno=’123’; should be $idno='123';
Now, come to the point about why second curl is not functioning!!! What is the return doing after your first curl request??
$Response = curl_exec($curl);
return $Response; <--- its returning you back from here
That's why the second curl is always unreachable!! Remove that unwanted return block.
I have found a new way to rewrite the code for multi-curl requests. This is well defined in the cURL documentation here:
http://us2.php.net/manual/en/function.curl-multi-exec.php

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

Categories