Php check if url have http

Question: I’m trying to build a basic «status» page using php that will tell my users if various services (webpages we use) are at least serving up pages (which isn’t 100% guarantee of working but its pretty good indicator) what i would like to do is something like www.domainname.com/mediawiki/index.php and make sure that returns the page or not I’m pretty new to php Solution 2: Try this: Note that your must activate Good luck Solution 3: Check out file_get_contents It will return the web page source as a string.

Here is what I want to do..

Lets say I am looking for the link «example.com» in a file at http://example.com/test.html».

I want to take a PHP script that looks for an in the mentioned website. However, I also need it to work if there is a class or ID tag in the .

How can I check if a url exists via PHP?

$file = 'http://www.domain.com/somefile.jpg'; $file_headers = @get_headers($file); if($file_headers[0] == 'HTTP/1.1 404 Not Found') < $exists = false; >else

From here: http://www.php.net/manual/en/function.file-exists.php#75064

. and right below the above post, there’s a curl solution:

Update code:-

You can use SimpleHtmlDom Class for find id or class in tag

Here is what I have found in case anyone else needs it also!

 $url = "http://example.com/test.html"; $searchFor = "example.com" $input = @file_get_contents($url) or die("Could not access file: $url"); $regexp = "]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)"; if(preg_match_all("/$regexp/siU", $input, $matches, PREG_SET_ORDER)) < foreach($matches as $match) < echo $match[2]; if ($match[2] == $searchFor) < $isMatch = 1; >else < $isMatch= 0; >// $match[0] = A tag // $match[2] = link address // $match[3] = link text > > if ($isMatch) < echo "

The page specified does contain your link. You have been credited the award amount!

"; > else < echo "

The specified page does not have your referral link.

"; >

Validating a URL in PHP, It depends on your definition of valid. Semantically valid, domain name resolves, etc. The quick approach would be to use preg_match to test the url against a good regular expression to validate it’s of the correct format. There appear to be some good examples on this thread PHP validation/regex for URL. …

Test if webpage is responding via php

I’m trying to build a basic «status» page using php that will tell my users if various services (webpages we use) are at least serving up pages (which isn’t 100% guarantee of working but its pretty good indicator)

what i would like to do is something like

www.domainname.com/mediawiki/index.php and make sure that returns the page or not

I’m pretty new to php so im not even sure what function im looking for.

There are ways to use built-in PHP functions to do this (e.g. file_get_contents), but they aren’t very good. I suggest you take a look at the excellent cURL library. This might point you in the right direction: Header only retrieval in php via curl

Since you just want to see if a page is «up» you don’t need to request the whole page, you can just use a HEAD request to get the headers for the page.

Note that your php.ini must activate allow_url_fopen

Check out file_get_contents

It will return the web page source as a string. This way you could even search the string for a specific value, if so desired, for finer results. This can be very useful in case content is still returned but is some sort of error message.

$somePage = file_get_contents('http://www.domainname.com/mediawiki/index.php'); // $somePage now contains the HTML source or false if failed 

Ensure allow_url_fopen = On in your php.ini

If you need to check the response headers, you can use $http_response_header

Another option would be to see of the socket is responding. (I can’t remember where I got this from originally but it lets me know if port 80 is responding). You could always direct this to a different port.

function server($addr) < if(strstr($addr,'/'))return $addr; >; $link = 'secure.sdinsite.net:'; $s_link = str_replace('::', ':', $link); $address = explode (':',"$s_link"); $churl = @fsockopen(server($addrress[0]), 80, $errno, $errstr, 20); if (!$churl) < $status = 'dead'; >else < $status = 'live'; >; echo $status; 

Источник

Question: I’m trying to build a basic «status» page using php that will tell my users if various services (webpages we use) are at least serving up pages (which isn’t 100% guarantee of working but its pretty good indicator) what i would like to do is something like www.domainname.com/mediawiki/index.php and make sure that returns the page or not I’m pretty new to php Solution 2: Try this: Note that your must activate Good luck Solution 3: Check out file_get_contents It will return the web page source as a string.

Here is what I want to do..

Lets say I am looking for the link «example.com» in a file at http://example.com/test.html».

I want to take a PHP script that looks for an in the mentioned website. However, I also need it to work if there is a class or ID tag in the .

How can I check if a url exists via PHP?

$file = 'http://www.domain.com/somefile.jpg'; $file_headers = @get_headers($file); if($file_headers[0] == 'HTTP/1.1 404 Not Found') < $exists = false; >else

From here: http://www.php.net/manual/en/function.file-exists.php#75064

. and right below the above post, there’s a curl solution:

Update code:-

You can use SimpleHtmlDom Class for find id or class in tag

Here is what I have found in case anyone else needs it also!

 $url = "http://example.com/test.html"; $searchFor = "example.com" $input = @file_get_contents($url) or die("Could not access file: $url"); $regexp = "]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)"; if(preg_match_all("/$regexp/siU", $input, $matches, PREG_SET_ORDER)) < foreach($matches as $match) < echo $match[2]; if ($match[2] == $searchFor) < $isMatch = 1; >else < $isMatch= 0; >// $match[0] = A tag // $match[2] = link address // $match[3] = link text > > if ($isMatch) < echo "

The page specified does contain your link. You have been credited the award amount!

"; > else < echo "

The specified page does not have your referral link.

"; >

Validating a URL in PHP, It depends on your definition of valid. Semantically valid, domain name resolves, etc. The quick approach would be to use preg_match to test the url against a good regular expression to validate it’s of the correct format. There appear to be some good examples on this thread PHP validation/regex for URL. …

Test if webpage is responding via php

I’m trying to build a basic «status» page using php that will tell my users if various services (webpages we use) are at least serving up pages (which isn’t 100% guarantee of working but its pretty good indicator)

what i would like to do is something like

www.domainname.com/mediawiki/index.php and make sure that returns the page or not

I’m pretty new to php so im not even sure what function im looking for.

There are ways to use built-in PHP functions to do this (e.g. file_get_contents), but they aren’t very good. I suggest you take a look at the excellent cURL library. This might point you in the right direction: Header only retrieval in php via curl

Since you just want to see if a page is «up» you don’t need to request the whole page, you can just use a HEAD request to get the headers for the page.

Note that your php.ini must activate allow_url_fopen

Check out file_get_contents

It will return the web page source as a string. This way you could even search the string for a specific value, if so desired, for finer results. This can be very useful in case content is still returned but is some sort of error message.

$somePage = file_get_contents('http://www.domainname.com/mediawiki/index.php'); // $somePage now contains the HTML source or false if failed 

Ensure allow_url_fopen = On in your php.ini

If you need to check the response headers, you can use $http_response_header

Another option would be to see of the socket is responding. (I can’t remember where I got this from originally but it lets me know if port 80 is responding). You could always direct this to a different port.

function server($addr) < if(strstr($addr,'/'))return $addr; >; $link = 'secure.sdinsite.net:'; $s_link = str_replace('::', ':', $link); $address = explode (':',"$s_link"); $churl = @fsockopen(server($addrress[0]), 80, $errno, $errstr, 20); if (!$churl) < $status = 'dead'; >else < $status = 'live'; >; echo $status; 

Источник

thagxt / url-check.php

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

// With this function you can check response code if it really is 404 or nah
function isValidUrl ( $ url )
// first do some quick sanity checks:
if (! $ url || !is_string( $ url ))
return false ;
>
// quick check url is roughly a valid http request: ( http://blah/. )
if ( ! preg_match( ‘/^http(s)?:\/\/[a-z0-9-]+(\.[a-z0-9-]+)*(:8+)?(\/.*)?$/i’ , $ url ) )
return false ;
>
// the next bit could be slow:
if (getHttpResponseCode_using_curl( $ url ) != 200 )
// if(getHttpResponseCode_using_getheaders($url) != 200) < // use this one if you cant use curl
return false ;
>
// all good!
return true ;
>
function getHttpResponseCode_using_curl ( $ url , $ followredirects = true )
// returns int responsecode, or false (if url does not exist or connection timeout occurs)
// NOTE: could potentially take up to 0-30 seconds , blocking further code execution (more or less depending on connection, target site, and local timeout settings))
// if $followredirects == false: return the FIRST known httpcode (ignore redirects)
// if $followredirects == true : return the LAST known httpcode (when redirected)
if (! $ url || ! is_string( $ url ))
return false ;
>
$ ch = @curl_init( $ url );
if ( $ ch === false )
return false ;
>
@curl_setopt( $ ch , CURLOPT_HEADER , true ); // we want headers
@curl_setopt( $ ch , CURLOPT_NOBODY , true ); // dont need body
@curl_setopt( $ ch , CURLOPT_RETURNTRANSFER , true ); // catch output (do NOT print!)
if ( $ followredirects )
@curl_setopt( $ ch , CURLOPT_FOLLOWLOCATION , true );
@curl_setopt( $ ch , CURLOPT_MAXREDIRS , 10 ); // fairly random number, but could prevent unwanted endless redirects with followlocation=true
> else
@curl_setopt( $ ch , CURLOPT_FOLLOWLOCATION , false );
>
// @curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,5); // fairly random number (seconds). but could prevent waiting forever to get a result
// @curl_setopt($ch, CURLOPT_TIMEOUT ,6); // fairly random number (seconds). but could prevent waiting forever to get a result
// @curl_setopt($ch, CURLOPT_USERAGENT ,»Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1″); // pretend we’re a regular browser
@curl_exec( $ ch );
if (@curl_errno( $ ch )) < // should be 0
@curl_close( $ ch );
return false ;
>
$ code = @curl_getinfo( $ ch , CURLINFO_HTTP_CODE ); // note: php.net documentation shows this returns a string, but really it returns an int
@curl_close( $ ch );
return $ code ;
>
function getHttpResponseCode_using_getheaders ( $ url , $ followredirects = true )
// returns string responsecode, or false if no responsecode found in headers (or url does not exist)
// NOTE: could potentially take up to 0-30 seconds , blocking further code execution (more or less depending on connection, target site, and local timeout settings))
// if $followredirects == false: return the FIRST known httpcode (ignore redirects)
// if $followredirects == true : return the LAST known httpcode (when redirected)
if (! $ url || ! is_string( $ url ))
return false ;
>
$ headers = @get_headers( $ url );
if ( $ headers && is_array( $ headers ))
if ( $ followredirects )
// we want the the last errorcode, reverse array so we start at the end:
$ headers = array_reverse( $ headers );
>
foreach ( $ headers as $ hline )
// search for things like «HTTP/1.1 200 OK» , «HTTP/1.0 200 OK» , «HTTP/1.1 301 PERMANENTLY MOVED» , «HTTP/1.1 400 Not Found» , etc.
// note that the exact syntax/version/output differs, so there is some string magic involved here
if (preg_match( ‘/^HTTP\/\S+\s+(627)\s+.*/’ , $ hline , $ matches ) ) < // "HTTP/*** ### ***"
$ code = $ matches [ 1 ];
return $ code ;
>
>
// no HTTP/xxx found in headers:
return false ;
>
// no headers :
return false ;
>

Источник

Читайте также:  Java arraylist string constructor
Оцените статью