Which HTTP status codes should we consider for dead links - php

We are executing below curl call from PHP.
$url = $fullurl;
if (isset($url)) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch , CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
$headers = curl_getinfo($ch);
curl_close($ch);
$check_url_status = $headers['http_code'];
if ($check_url_status == '200')
$ress = "Link Works";
else
$ress = "Broken Link";
}
What other HTTP status codes should we consider to check if the URL is not a broken /dead link.

Remember the 5 HTTP Status code classes : 1xx Continue (protocol switching), 2xx OK, 3xx Redirect, 4xx client error, 5xx server error.
If your Curl client follow the redirections (3xx), I think you can just test that status code <= 299. All other status code will make a "broken link".
Depending on how deep is your test, you can also think of theses cases :
401 Unauthorized/ 403 Forbidden : the ressource need authentification. It does not mean the link is broken, but that authorized client may see it, and other will not.
204 No Content : the ressource is accessible but does not return any content. Some analytics ressources returns 204. But the visual result will be a broken image or a link to an empty page.
If your goal is to change the display of a broken link you can use Javascript to manage it client-side, but it can be limited to your domain. See this question

Related

Why is my REST request to Google's custom search engine API returning a 400 error?

When I perform the request, I am met with a 400 error stating that "our client has issued a malformed or illegal request. That’s all we know.". All of my keys are valid and the website's url that I'm using to issue the request has been verified. When I input the url into my browser, it gives me a perfect response full of relevant data.
Code Snippet:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.googleapis.com/customsearch/v1?
key=SERVER_KEY&cx=SEARCH_ENGINE_ID&q=flower&searchtype=image");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$file = curl_exec($ch);
curl_close($ch);
echo $file;
It was a sneaky syntactical error all along. My text editor was auto-spacing after the SERVER_KEY, which made the API request null

to execute a link using curl

i am trying to execute a link (without page being redirected) using curl.
see below my code...
$ch = curl_init(); // Initializing
//curl_setopt($ch, CURLOPT_URL, trim("http://api.smsgatewayhub.com/smsapi/pushsms.aspx?user=stthomasmtc&pwd=429944&to=9176411081&sid=STMTSC&msg=Dear Sam,%20choir%20practice%20will%20be%20held%20in%20our%20Church%20on%20July%2031%20at%208:00%20pm.%20Thanks,%20St.%20Thomas%20MTC!&fl=0&gwid=2")); // Set URI
curl_setopt($ch, CURLOPT_URL,"http://api.smsgatewayhub.com/smsapi/pushsms.aspx?user=stthomasmtc&pwd=429944&to=9176411081&sid=STMTSC&msg=Dear Sam,%20choir%20practice%20will%20be%20held%20in%20our%20Church%20on%20July%2031%20at%208:00%20pm.%20Thanks,%20St.%20Thomas%20MTC!&fl=0&gwid=2"); // Set URI
curl_setopt($ch, CURLOPT_HEADER, 0); //Set Header
curl_setopt($ch, CURLOPT_TIMEOUT, 300); // Time-out in seconds
$result = curl_exec($ch); // Executing
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpCode != 200) {
$result = ""; // Executing
}
curl_close($ch); // Closing the channel
return $result;
but i get the response as Bad Request.
when i try to change the url to www.google.com, it seems executing. When i manually use this link in browser, its executed as expected - to send message to me. let me know if there is a better way to execute a link without page being redirected...
This maybe a very old question. But since it has gone unanswered and pops up in Google when you say SMSGatewayHub + SO, I'll go ahead and present an alternative.
Get the class https://github.com/adarshdec23/SMSGatewayhub. It works with both promotional and transactional SMS messages.
Here is a step by step guide to using it.
It uses an API key instead of your username and password. Once you're done with that, the class makes an API call (without cURL). Its simple and gets the job done.

How can I output the body of a webpage if the response code is 500 using curl?

I've been through hell and high water with this problem. I get a 500 error on a page a tiny, tiny fraction of the time. I have been completely unable to reproduce it, but Google insists that they see a 500 code. Fetch as Googlebot says it's successful, however something is wrong. I've been down many avenues and the only recourse I have left is to brute-force the local copy of the website.
I want to use curl to hammer the dev site until I get a 500 error, and when I do, to output the body of the page to the terminal so I can actually get some useful information.
for(;;){
$url = "http://www.blahblah.dev/";
$ch = curl_init();
//Set the URL
curl_setopt($ch, CURLOPT_URL, $url);
//Enable curl response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//Enable POST data
curl_setopt($ch, CURLOPT_POST, true);
//Use the $pData array as the POST data
curl_setopt($ch, CURLOPT_POSTFIELDS, $jData);
$result = curl_exec($ch);
if(strstr($result, "error")){
echo $result;
exit();
}
curl_close($ch);
usleep(500000);
}
As you can see, I'm simply checking to see if "error" appears in the body, as I can't figure out how to check for a 500 error properly. I realize that this is a terrible and contrived way of debugging, but it's all I've got at this point. Thanks!

Handle 403 error without making redirection

I wanted to handle 403 error without using server side error redirection methods(i.e. without using .htaccess file). Sometimes server returns a 403 error message (forbidden access). So is it possible to have PHP script which handles this 403 error message?
For example, Before showing error page, I would like to obtain server status when it my specific php page runs, and without making a redirection, I would just like to display custom message in the that page.
Some solutions for you.
Check for URL errors and make sure the actual web page is specified. Its common reason for a web site to return the 403 Forbidden error, when the URL is pointing to a directory instead of a web page. Which can be done using HttpRequest Class in PHP. You can use http_get to perform GET request. You can also Test URL here.
<?php
$response = http_get("URL", array("timeout"=>1), $info);
print_r($info);
?>
Output:
array (
'effective_url' => 'URL',
'response_code' => 403,
.
and so on
)
What is important for you is response_code with which you can play further.
Use of curl.
function http_response($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$head = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if(!$head)
{
return FALSE;
}
return $httpCode;
}
$errorcode = http_response("URL"); //if success 200 otherwise different

How to test if a remote server is down for maintenance even though there may be a redirect to a maintenance page?

We have an application that is dependent upon a remote service. I have been asked to implement some code whereby if the remote web server is down (due to maintenance or glithes) that I display an appropriate message.
The issue at hand is that when the remote is down for maintenance, they usually redirect to another page. So how do I get about implementing in PHP a robust function that can tell if a particular URL is up and running as opposed to it being redirected to a dummy page.
Thank You
Just check the response text. If the the response contains any text that is present in the redirected url, Then its surely in maintenance mode.
If the remote web server is down you can check it too. see https://stackoverflow.com/questions/9144825/php-check-if-a-site-is-down/9145124#9145124
Just check the HTTP return code. This is possible with curl for instance:
CURLINFO_HTTP_CODE
http://de2.php.net/manual/en/function.curl-getinfo.php
<?php
$success = 0;
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
curl_exec($ch);
// Check if any error occured
if(!curl_errno($ch))
{
if(curl_getinfo($c, CURLINFO_HTTP_CODE) === 200)
$success = 1;
}
// close cURL resource, and free up system resources
curl_close($ch);
?>

Categories