How can I check if a URL exists via PHP?
Here:
$file = 'http://www.example.com/somefile.jpg';
$file_headers = @get_headers($file);
if(!$file_headers || $file_headers[0] == 'HTTP/1.1 404 Not Found') {
$exists = false;
}
else {
$exists = true;
}
From here and right below the above post, there's a curl solution:
function url_exists($url) {
return curl_init($url) !== false;
}
Check if a remote page exists using PHP?
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10); //follow up to 10 redirections - avoids loops
$data = curl_exec($ch);
curl_close($ch);
if (!$data) {
echo "Domain could not be found";
}
else {
preg_match_all("/HTTP\/1\.[1|0]\s(\d{3})/",$data,$matches);
$code = end($matches[1]);
if ($code == 200) {
echo "Page Found";
}
elseif ($code == 404) {
echo "Page Not Found";
}
}
Modified version of code from here.
How to check if a file exists from a url
You have to use CURL
function does_url_exists($url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($code == 200) {
$status = true;
} else {
$status = false;
}
curl_close($ch);
return $status;
}
How to check a url exist or not in php
if(! @ file_get_contents('http://www.domain.com/file.txt')){
echo 'path doesn't exist';
}
This is the easiest way to do it. If you are unfamiliar with the @
, that will instruct the function to return false if it would have otherwise thrown an error
What is the best way to check if a URL exists in PHP?
You can use get_headers($url)
Example 2 from Manual:
<?php
// By default get_headers uses a GET request to fetch the headers. If you
// want to send a HEAD request instead, you can do so using a stream context:
stream_context_set_default(
array(
'http' => array(
'method' => 'HEAD'
)
)
);
print_r(get_headers('http://example.com'));
// gives
Array
(
[0] => HTTP/1.1 200 OK
[Date] => Sat, 29 May 2004 12:28:14 GMT
[Server] => Apache/1.3.27 (Unix) (Red-Hat/Linux)
[Last-Modified] => Wed, 08 Jan 2003 23:11:55 GMT
[ETag] => "3f80f-1b6-3e1cb03b"
[Accept-Ranges] => bytes
[Content-Length] => 438
[Connection] => close
[Content-Type] => text/html
)
The first array element will contain the HTTP Response Status code. You have to parse that.
Note that the get_headers
function in the example will issue an HTTP HEAD request, which means it will not fetch the body of the URL. This is more efficient than using a GET request which will also return the body.
Also note that by setting a default context, any subsequent calls using an http stream context, will now issue HEAD requests. So make sure to reset the default context to use GET again when done.
PHP also provides the variable $http_response_header
The
$http_response_header
array is similar to theget_headers()
function. When using the HTTP wrapper,$http_response_header
will be populated with the HTTP response headers.$http_response_header
will be created in the local scope.
If you want to download the content of a remote resource, you don't want to do two requests (one to see if the resource exists and one to fetch it), but just one. In that case, use something like file_get_contents
to fetch the content and then inspect the headers from the variable.
PHP - Check if the final URL exists
You can tell cURL
to follow all redirects, and return the result from the final redirection. Use:
curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true);
How to check if page exists?
To read content from other pages i`m using CURL and do it like that
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
$page_content = curl_exec($ch);
curl_close($ch);
if ($page_content != "false") {
// do something with content
} else {
// this page is not available
}
Related Topics
Session_Start() Creates New Session Every Refresh
Why Are Escape Characters Being Added to the Value of the Hidden Input
Dealing with Special Characters in Object Property Names
Blank Spaces in Column Names with MySQL
Does PHP Close the File After the File Handler Is Garbage Collected
Differencebetween Is_A and Instanceof
"Error 404 Not Found" in Magento Admin Login Page
Update Xampp from Maria Db 10.1 to 10.2
Insert Non-English Decimal Points in MySQL
What Are the Differences Between "PHP Artisan Dump-Autoload" and "Composer Dump-Autoload"
Check If Current User Is Administrator in Wordpress
PHP Method="Post" Stopped Working After I Added This .Htaccess... Why
Differences and Similarities Between Lumen and Laravel
Find Duplicate Value in Multi-Dimensional Array