Welcome to WebmasterWorld Guest from 54.198.147.221

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Check if a page has keyword and output something

     
1:02 am on Apr 13, 2012 (gmt 0)

New User

5+ Year Member

joined:Mar 24, 2012
posts:11
votes: 0


Hello again!

I am back with another question about PHP.

I was just wondering if I could check a page and if that page has the words "Online" then on my website, i would put <a href="#">Go to server</a>. But if it is "Offline" then i would display <p>Sorry, but it is off</p>. This is not really what i would use but just to illustrate what i want to do... there it is.

So basically i am getting something from an external page and then changing the content on MY page based on what that ext. page says.

Dont know if you understand, please if you dont ask me to clarify

[edited by: eelixduppy at 12:31 pm (utc) on Apr 13, 2012]
[edit reason] removed URL [/edit]

5:56 pm on Apr 13, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 12, 2005
posts:5966
votes: 0


The short and sweet way would be to do this:


$url = "http://www.example.com/path/to/script";

$content = file_get_contents($url);
if(strpos($content, 'Online') !== 0) {
// it says online
} else {
// it doesn't
}


Hope this helps.
6:27 pm on Apr 13, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 12, 2005
posts:5966
votes: 0


Addendum:

However, if I were you I would go directly to the source for a check to see whether it is up or not. If you can (e.g., if its not behind some firewall or something) I would just ping the server from PHP to figure out the status. Why go through some third-party like this?
8:36 pm on Apr 13, 2012 (gmt 0)

New User

5+ Year Member

joined:Mar 24, 2012
posts:11
votes: 0


Hmmm.... interesting. What im trying to see if its online or not is somesite.com:24844. If its offline, the page doesnt show and if its online, then it will display a page saying SHOUTcast blablabla. If i could do it this way, that would be great.

Edit: Whoops sorry if i wasnt supposed to put the url. Its fixed now :P
4:46 am on Apr 14, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 12, 2005
posts:5966
votes: 0


You can use curl to get the response (or lack thereof) if it is a web server. Something like this perhaps:


$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "http://www.example.com");
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_PORT, 24844);

if(curl_exec($ch) && curl_getinfo($ch, CURLINFO_HTTP_CODE) == 200) {
// server seems to be up
} else {
// server is down or there is an issue with the page
}

curl_close($ch);
1:11 am on Apr 15, 2012 (gmt 0)

New User

5+ Year Member

joined:Mar 24, 2012
posts:11
votes: 0


Well, that code works. Problem is that when its online, it returns

ICY 401 Service Unavailable icy-notice1:
SHOUTcast Distributed Network Audio Server/Linux v1.9.8
icy-notice2:The resource requested is currently unavailable
Offline

My code below:
<?
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "MYSERVER");
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_PORT, 24844);

if(curl_exec($ch) && curl_getinfo($ch, CURLINFO_HTTP_CODE) == 200) {
// server seems to be up
echo 'Online';

} else {
// server is down or there is an issue with the page
echo 'Offline';
}

curl_close($ch);
?>

I think its because its a SHOUTcast server, and not a web server. Anything that works with a SHOUTcast?

btw: When its offline and i go to serv.com:12345, its like if that page doesnt exist (like going to aosdhaasdcasdca.com) and when its online and i go to the url, its like going to google.com (if you know what i mean)
6:04 pm on Apr 28, 2012 (gmt 0)

New User

5+ Year Member

joined:Mar 24, 2012
posts:11
votes: 0


anybody?
2:08 am on Apr 29, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 12, 2005
posts:5966
votes: 0


401 means you aren't authenticating correctly, so you may have to send some credentials in the header. For example:


curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members