How can I check if RESTAPI is down using curl php - php

I wants to make a virtual environment to check if web-service is down.
Case: If web-service is down for 7 seconds then system should re-try once and if not work then it should send the notification to admin.
$ch = curl_init();
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_URL, $url);
// Use 0 to wait indefinitely.
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 7);
// The maximum number of seconds to allow cURL functions to execute
curl_setopt($ch, CURLOPT_TIMEOUT, 7);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
//curl_setopt($ch, CURLOPT_NOBODY, TRUE); // remove body
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$responseData = curl_exec($ch);
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
$httpCode = curl_getinfo($ch);
curl_close($ch);

You could try this:
$url = 'my.url';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($retcode == 200)
{
// It's working
}
else
{
// It's down
}

Finally, I found the solution.
function checkData($loop = 0){
$url = 'http://google.co.in:8080/';
$ch = curl_init();
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_URL, $url);
// Use 0 to wait indefinitely.
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 7);
// The maximum number of seconds to allow cURL functions to execute
curl_setopt($ch, CURLOPT_TIMEOUT, 7);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
//curl_setopt($ch, CURLOPT_NOBODY, TRUE); // remove body
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$responseData = curl_exec($ch);
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
$httpCode = curl_getinfo($ch);
curl_close($ch);
if ($curl_errno > 0){
if($loop > 1 ){
//send mail to admin
}else{
$loop++;
sleep(7);
checkData();
}
}
}
checkData();

Related

Why is the return access token not valid?

This code not saving access token after verifying that this access token is valid or not instead of that it returns i=1. Eight hours ago this script work fine but it suddenly stop working.
<?php
function get_json($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FAILONERROR, 0);
$data = curl_exec($ch);
curl_close($ch);
return json_decode($data);
}
function get_html($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FAILONERROR, 0);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$token2 = $_GET["user"];
session_start();
if(isset($token2)){
if(preg_match("'access_token=(.*?)&expires_in='", $token2, $matches)){
$token = $matches[1];
}else{
$token = $token2;}
$exe = json_decode(get_html("https://graph.facebook.com/app?access_token=".$token ))->id;
$extend = get_html("https://graph.facebook.com/me/permissions?access_token=" . $token);
if($extend == true){
$pos = strpos($extend, "publish_actions");
if ($pos == true) {
$_SESSION['token'] = $token;
$ch = curl_init('http://my site/saver.php');
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, "token=".$token);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_exec ($ch);
curl_close ($ch);
header('Location:index.php');
}
else {
header('Location:index.php?i=1');}
//access token is not valid
}else{
header('Location:index.php?i=4');}
}else{
header('Location:index.php?i=2');}
?>

Using curl in https

I tried accessing a website using curl and it just output Object moved to here. I used curl_error but doesn't show any errors.
$url = "https:somewebsite.com";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // Accepts all CAs
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
//curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
if(curl_error($ch))
{
echo 'error:' . curl_error($ch);
}
curl_close($ch);
echo $output;
?>

How to debug a get request in php using curl

I'm trying to make a get request in php using curl. This is what I'm doing:
$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "username:password");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
printf($result);
But $result doesn't print out anything, no success or failure message. I've successfully reached the endpoint via postman and in a web browser so I know it works. Printing out $curl prints: "Resource #1" which makes me think curl is properly installed on the server.
I'm not sure what steps to take next to make things work.
Add a few more option for troubleshooting purposes.
Check for an error response.
If no error, get the details:
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT,10);
curl_setopt($ch, CURLOPT_FAILONERROR,true);
curl_setopt($ch, CURLOPT_ENCODING,"");
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$data = curl_exec($ch);
if (curl_errno($ch)){
$data .= 'Retreive Base Page Error: ' . curl_error($ch);
}
else {
$skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE));
$head = substr($data,0,$skip);
$data = substr($data,$skip);
$info = curl_getinfo($ch);
$info = var_export($info,true);
}
echo $head;
echo $info;
You can utilize curl's CURLOPT_VERBOSE and CURLOPT_STDERR like this:
$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "username:password");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$verbose = fopen('php://temp', 'w+');
curl_setopt($curl, CURLOPT_VERBOSE, true);
curl_setopt($curl, CURLOPT_STDERR, $verbose);
$result = curl_exec($curl);
curl_close($curl);
rewind($verbose);
$verboseLog = stream_get_contents($verbose);
echo "Verbose information:\n<pre>", htmlspecialchars($verboseLog), "</pre>\n";
printf($result);

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

PHP How can I open several sources using curl?

I have some code to get json content of a site1 but I also need to get content of a site2. Should I rewrite all these lines again for the site2? Or maybe I can add one more URL in the curl_setopt?
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://site1.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$outputJson = curl_exec($ch);
if ($outputJson === FALSE) {
echo 'Sorry, This service is currently unavailable: '. curl_error($ch);
}
You can create a function like
function get_data($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$outputJson = curl_exec($ch);
if ($outputJson === FALSE) {
echo 'Sorry, This service is currently unavailable: '. curl_error($ch);
}
return $outputJson;
}
and call it with
get_data("http://blah.com");
get_data("http://blah1.com");
This might not be an optimal solution but shuould work for simple instances
You can get better performance with multi url curl.
See :
http://php.net/manual/en/function.curl-multi-exec.php
And :
http://www.rustyrazorblade.com/2008/02/curl_multi_exec/
You might want to try to loop trought the different site:
$aSites = array("http://site1.com","http://site2.com");
for($x=0; $x<count($aSites); $x++){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$aSites[$x]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$outputJson = curl_exec($ch);
if ($outputJson === FALSE) {
echo 'Sorry, This service is currently unavailable: '. curl_error($ch);
}
}
<?
$url1 = "http://site1.com";
$url2 = "http://site2.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$outputJson = curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, $url2);
$outputJson2 = curl_exec($ch);
curl_close($ch);
if ($outputJson === FALSE || $outputJson2 === FALSE) {
echo 'Sorry, This service is currently unavailable: '. curl_error($ch);
}
?>

Categories