Nibhrit
Nibhrit

Reputation: 188

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.

Upvotes: 0

Views: 17268

Answers (1)

bariz
bariz

Reputation: 124

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

Upvotes: 1

Related Questions