homepage Welcome to WebmasterWorld Guest from 54.234.59.94
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Check if a page has keyword and output something
psl101




msg:4440284
 1:02 am on Apr 13, 2012 (gmt 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]

 

eelixduppy




msg:4440584
 5:56 pm on Apr 13, 2012 (gmt 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.

eelixduppy




msg:4440597
 6:27 pm on Apr 13, 2012 (gmt 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?

psl101




msg:4440640
 8:36 pm on Apr 13, 2012 (gmt 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

eelixduppy




msg:4440740
 4:46 am on Apr 14, 2012 (gmt 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);

psl101




msg:4440968
 1:11 am on Apr 15, 2012 (gmt 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)

psl101




msg:4446948
 6:04 pm on Apr 28, 2012 (gmt 0)

anybody?

eelixduppy




msg:4447085
 2:08 am on Apr 29, 2012 (gmt 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);

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved