views:

86

answers:

2

Hi,

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.

Thanks if anyone could help me out.

+1  A: 

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
?>
zneak
How would I record the results into my database onto the correct column?
Raymond
@Raymond Use `UPDATE` statements. Do you have some basic knowledge of the SQL language?
zneak
zneak, not really but a bit. I under the update statement. Give me a structure.
Raymond
@Raymond `UPDATE toys SET checks = ? WHERE id = ?`, where you have to fill in the blanks of `checks` (with either 0 or 1 depending on if the page is alive or not), and `id` (with the row's id).
zneak
A: 

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);
aularon
I will be getting URLS from the database, once results display it records to the MYSQL database. Thats what im needing.
Raymond
@Raymond get them from the DB, loop the records, and update accordingly as @zneak said.
aularon