views:

35

answers:

1

Basically, I have multiple URL's stored in a MySQL table. I want to pull those URLs from the table and have cURL connect to all of them. Currently I've been storing the URL's in the local script, but I've added a new page that I can add and remove them from the database, and I'd like the page to reflect it appropriately.

Here is what I currently have:

  $sites[0]['url'] = "http://example0.com ";
  $sites[1]['url'] = "http://example1.com";
  $sites[2]['url'] = "http://example2.com";
  $sites[3]['url'] = "http://example3.com";

  foreach($sites as $s) 
  {
   // Now for some cURL to run it.
   $ch = curl_init($s['url']); //load the urls and send GET data
   curl_setopt($ch, CURLOPT_TIMEOUT, 2); //No need to wait for it to load. Execute it and go.
   curl_exec($ch); //Execute
   curl_close($ch); //Close it off 
  }

Now I assume it can't be too amazingly difficult to do, I just don't know how. So if you could point me in the right direction, I'd be grateful. But if you supply me with some code, please comment it appropriately so that I can understand what each line is doing.

+1  A: 

Assuming you have a MySQL connection set up, here's the code that will do it for you:

/* Define $SQL variable being a dataset returned from the database.
The SELECT url FROM urls means "fetch the column 'url' for each row 
in the table 'urls' or generate an error" */
$SQL = mysql_query("SELECT url FROM urls") or die(mysql_error());
/* Loop through the dataset and create a new variable which is an
array (loopable) that contains the fetched data */
while($resultSet = mysql_fetch_array($SQL)){
    // Now for some cURL to run it.
   $ch = curl_init($resultSet['url']); //load the urls and send GET data
   curl_setopt($ch, CURLOPT_TIMEOUT, 2); //No need to wait for it to load. Execute it and go.
   curl_exec($ch); //Execute
   curl_close($ch); //Close it off 
}
Jonatan Littke
Thank you so much for all the comments. One second, I seem to have broken my code somewhere and have a 500 error. Once I fix it I'll test this out, though it definitely looks like it'll work.
Rob
Works flawlessly. Thank's a lot, especially for the comments.
Rob
You're welcome :)
Jonatan Littke