how to connect to a remote ip?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
saumya
Forum Contributor
Posts: 193
Joined: Sun Jan 30, 2005 10:21 pm

how to connect to a remote ip?

Post by saumya »

how can i connect to a remote ip such as http://www.google.com
What i want is open the site and take all the contents from that site and show it in my page.
127.0.0.1
Forum Newbie
Posts: 22
Joined: Mon Sep 12, 2005 8:59 pm

Post by 127.0.0.1 »

There are loads of ways to achieve this.

I preffer to use curl when "taking content that is not mines".

with curl it makes it easy to fake HTTP referrer and HTTP agent.

Example

Code: Select all

$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, "http://www.google.com/search?q=php");
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; U; FreeBSD i386; en-US; rv:1.2a)");
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
$GoogleContent = curl_exec ($ch);
curl_close($ch);
I hope I got the code right this time and it has came out ok. I am knew to this forum.

There are a lot more features to curl its worth checking out.

http://uk.php.net/curl

As for working with the data it depends what part you are wanting to strip out. 2 Options you can explode into an array and work with the data there or use regular expressions. Regular expressions is probably your best bet.

Oh one last thing if you use curl make sure you set the useragent to something otherwise it will default to curl and youll get detected very easily.
saumya
Forum Contributor
Posts: 193
Joined: Sun Jan 30, 2005 10:21 pm

Post by saumya »

thank you.
Post Reply