Welcome to WebmasterWorld Guest from 54.196.212.62

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

cURL

     

dcool86

4:50 pm on Mar 4, 2012 (gmt 0)



Hello,

I'm trying to check if a website has a piece of code on it. The code is below.

<!-- BEGIN TEST CODE -->

I want to see if that is in the source code. If it is it will echo yes if not it will echo no.


Thank you

dcool86

4:53 pm on Mar 4, 2012 (gmt 0)



What will be the quickest and most reliable way of doing that to. Thank you

brotherhood of LAN

11:00 pm on Mar 4, 2012 (gmt 0)

WebmasterWorld Administrator brotherhood_of_lan is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Do you need a method to fetch the page or a method to match the string?

strpos [php.net] is good for the comparison

PHP has support for cURL [php.net] and there are examples on that page.

dcool86

4:06 am on Mar 5, 2012 (gmt 0)



I was wondering if you could put together a code to look for

<!-- BEGIN TEST CODE -->


What I have written/tried doesn't work.


Thank you

dcool86

2:52 am on Mar 6, 2012 (gmt 0)



This is what I have so far

$ch = curl_init("http://www.example.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$text = curl_exec($ch);
$test = strpos($text, "<!-- BEGIN TEST CODE -->");
if ($test==false)
{
echo "yes";
}else{
echo "no";
}

I'm always getting a yes on any page even the one that has

<!-- BEGIN TEST CODE -->

what am I'm doing wrong?

dcool86

3:23 am on Mar 6, 2012 (gmt 0)



i got it to work but it doesn't seem to work on certain domains. Thanks

lostdreamer

2:38 pm on Mar 13, 2012 (gmt 0)

5+ Year Member





if(checkSite("http://www.google.nl"))
echo "Found";
else echo "not Found";

function checkSite($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$text = curl_exec($ch);
$test = strpos($text, "<!-- BEGIN TEST CODE -->");
if ($test === false)
return false;
else return true;
}


With strpos you should allways do a strict comparison ( === instead of == )

If the text would start with the text you are searching for, strpos will return 0 (since the search text starts at char 0)

if 0 == false returns TRUE
if 0 === false returns FALSE

This just might be why you script fails
On top of that, in your above script, the If Else logic is the wrong way around ;)

Regards,
LostDreamer

dcool86

7:45 pm on Mar 14, 2012 (gmt 0)



Thank you
 

Featured Threads

Hot Threads This Week

Hot Threads This Month