PHP - How to check URLS for 404/Timeout? - php

Here is my structure:
MYSQL: Table: toys ---> Column: id, URL. How do I get my PHP script to check all of those URLs to see if they are alive or have page 404's? Try not to echo or diplay the results on page. I will need to to record in MYSQL with a extra column "checks".
Results will be in this format:
http://asdasd.adas --- up --- 404
It will be in PHP/Curl if possible. I have been trying for ages. I gave up so decided to ask here.
URL's are all located in my database.

In cURL, there's the curl_getinfo function, that returns some info about the current handle:
<?php
// Create a curl handle
$ch = curl_init('http://www.yahoo.com/');
// Execute
curl_exec($ch);
//fill here the error/timeout checks.
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

I trust you're able to run a SQL query and enumerate through the results, so here's the cURL part. For each URL, send it a HEAD request, and check the result code.
<?php
$handle = curl_init($yourURL);
curl_setopt($handle, CURLOPT_NOBODY, true);
curl_exec($handle);
$result = curl_getinfo($handle, CURLINFO_HTTP_CODE);
// $result now contains the HTTP result code the page sent
?>

Related

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.

connecting to volusion store using curl functions

I have volusion store URL and I am connecting to volusion store using this URL and curl function. Following is my code to connect -
$URL
='http://v970032.y5pgm9yfhypo.demo17.volusion.com/net/WebService.aspx?Login=rashmi#edreamz.in&EncryptedPassword=24EA69124482A486AF3E6BA68DDEECBB7CBC3661EA792E0DC4A40CD6FC031E6E&Import=Update';
$ch = curl_init($URL);
I just want to check whether the connection for the given URL is connected or not. How can I achieve this please advise me. Right now if I used -
echo $ch
it is only displaying Resource id #2
thanks in advance.
First, curl_init ins't enough. You'll need to use curl_exec to actually send the request. Once the request is sent, you can use curl_errno to check if an error was returned and curl_getinfo to get more information about the request and response such as the HTTP response code (e.g.: 200, 404), how long it took to connect, etc.
Modified example from the curl_getinfo documentation:
<?php
// Create a curl handle
$ch = curl_init('http://v970032.y5pgm9yfhypo.demo17.volusion.com/net/WebService.aspx?Login=rashmi#edreamz.in&EncryptedPassword=24EA69124482A486AF3E6BA68DDEECBB7CBC3661EA792E0DC4A40CD6FC031E6E&Import=Update');
// Tell cURL to return the transfer instead of outputting it.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute
$result = curl_exec($ch);
// Check if any error occured
if (!curl_errno($ch)) {
// Here, I'm outputting the value of $result but you can do whatever you'd like
// with it such as using a DOMDocument to parse XML or HTML, json_decode to
// parse JSON, etc.
echo $result;
} else {
echo 'Sorry, an error occurred while connecting to Volusion.';
}
// Close handle
curl_close($ch);
Depending on what you're trying to do, you might want to have a look at curl_setopt and more specifically at CURLOPT_RETURNTRANSFER and some of the other basic options you can pass to cURL. The options are well documented.

Simple Issue With cURL

i am in the closing stages of an sms application that will send different sms messages to different phone numbers. I will be using an sms gateway and my research led me to use cURL to implement the smpp api of the company that has the gateway. I was doing a test run of cURL on my localhost to see if all went smoothly before i implemented on the application(i wanted it to insert values into a table), but it was not working. So i need help on where i am doing something wrong.Here is my code. Thanks.
include 'sms_connect.php';
$sql="select name from sms";
$result=mysqli_query($link,$sql);
while($row=mysqli_fetch_assoc($result))
{
$name=$row['name'];
$url = "http://localhost/sms/index.php?name=".$name;
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
curl_exec($ch);
}
// close cURL resource, and free up system resources
curl_close($ch);
Do
$results = $curl_exec($ch);
echo $results;
Look at the output. If you get the output you expect, the problem is when you insert the data in the database, if not - you have a problem with your cURL, and post the error message here.

Getting HTTP response of a URL in PHP - XML

I am trying to retrieve the status of the url. I am writing php code to retrieve it but i am not getting the output. Nothing is being displayed.
I am reading the url's from the xml file and storing it in variable. I am doing
file_get_contents($url);
echo $http_respone_header[0];
$url contains the url which i have read from the xml file.
The thing You are doing is not getting URL status but content of the site. In case of wrong/invalid URL file_get_contents() returns false as it is described in documentation in here
If You are trying to get status You can simply use the solution described in other topic on this site.
<?php
$url = 'http://www.wp.pl';
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
/* Get the HTML or whatever is linked in $url. */
$response = curl_exec($handle);
/* Check for 404 (file not found). */
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
echo $httpCode;
curl_close($handle);
?>
Link to the mentioned topic: here

Multi Curl Usage when number of requests is dynamic

I have a database setup and all it does is give me at most 10 URL's. I need to post data to those 10 URL's when the page is loaded. This means the script to send message hits "send.php?message=Foo". and it post's 'foo' to the pages in the database. There is no way around this. But I need to be able to do it. Right now I am trying to use regular curl requests in a while loop but that only posts to the first URL. How do I use the CURL multi functions to do this:
$query = 'SELECT * FROM `chat` LIMIT 0, 20;';
$result = mysql_query($query);
$init = 0;
while($row = mysql_fetch_array( $result )) {
$URL = $row['url'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"$URL");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_exec ($ch);
curl_close ($ch);
}
Are you sure it only posts to the first URL? Try adding some debugging statements. Check if you're hitting a timeout in PHP.
What about timeouts? Look at the example here http://au2.php.net/manual/en/function.curl-multi-init.php. Start a multi handle before while, create curl handles in while and add those handles to multi handle method, then after while execute the curl call and close the handle, do check for errors/exceptions thrown by curl, example, a url may time out and so on.

Categories