0 Replies - 1250 Views - Last Post: 27 September 2010 - 08:44 AM

#1 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6644
  • View blog
  • Posts: 30,937
  • Joined: 10-May 07

Get Return Statement from URL

Posted 27 September 2010 - 08:44 AM

Description: Pass the full URL to the functionPass a URL to a function, & return the httpd_status code, for example 301 moved, 404 not found, 200 success.
function get_url_status($URL) {
  if(empty($URL)) die("Please provide a valid URL");

  // create a new cURL resource
  $ch = curl_init();

  // set URL and other appropriate options
  curl_setopt($ch, CURLOPT_URL, $URL);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_HEADER, 1);
  curl_setopt($ch, CURLOPT_NOBODY, 1);

  // grab results and pass it to the buffer
  $buffer = @curl_exec($ch);

  // close cURL resource, and free up system resources
  curl_close($ch);

  // validate the buffer
  if (empty($buffer)) die("GET Failed");
  $status=array();
  // gather return code from buffer & plug into status array
  preg_match('/HTTP/.* ([0-9]+) .*/', $buffer, $status);
  // return the results
  return $status[1];
}


Is This A Good Question/Topic? 0
  • +

Page 1 of 1