Converge API - check if API is available - php

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);

Related

phpcurl losing connection after many refresh

am using this code to get access to myOwncloud server and it works fine but if i refresh the page i get this error "The requested URL returned error: 401 "
and if i wait 5 min or something it works again
any idea why?
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->url.($this->path ? '/'.$this->path : ''));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); /// 1
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_USERPWD, "$this->username:$this->password");
//PROPFIND request that lists all requested properties.
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PROPFIND');
curl_setopt($ch, CURLOPT_FAILONERROR, true); // Required for HTTP error codes to be reported via our call to curl_error($ch)
$response = curl_exec($ch);
if (false === $response) {
$response = curl_error($ch);
}
curl_close($ch);

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.

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.

php curl_exec returns empty

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXY, $proxy); // $proxy is ip of proxy server
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$httpCode = curl_getinfo($ch , CURLINFO_HTTP_CODE); // this results 0 every time
echo '<br> curl'.$ch; //this line outputs resource id#5
$exec = stripslashes(curl_exec($ch));
echo '<br> exec'.curl_exec($ch); //this results blank
i am confused why $exec does not return anything ,i am new to curl please help, thanks in advance
curl_exec will return false when the request failed. Adjust your function to this :
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXY, $proxy); // $proxy is ip of proxy server
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$httpCode = curl_getinfo($ch , CURLINFO_HTTP_CODE); // this results 0 every time
$response = curl_exec($ch);
if ($response === false)
$response = curl_error($ch);
echo stripslashes($response);
curl_close($ch);
This way u'll see the curl error
You're trying to access a HTTP response code before you actually make the HTTP call. Reverse the execution as follows:
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch , CURLINFO_HTTP_CODE); // this results 0 every time
Try:
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
Maybe the response is longer than 10.
I had the same issue, I solved like this.
The result return 0 mean that you can not connect to the server so please recheck your proxy and increase the timeout :)

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