Loading URL with CURL from specified IP address

Sometimes we have to automate some pages testing or loading content or whatever. CURL is an excelent library for any HTTP interfacing.

If you ever have to connect to a server and ask for particular server name you can do it with curl setopt parameters. If you want to get file /index.php from www.mytopsite.com from particular IP (without adding any /etc/hosts mappings) you can do it like this:

$headers = array("Host: www.myfaketopsite.com");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, "http://10.1.2.3/index.php");

This is really helpful if you have a farm of 40 web servers and you have to ask each and every one, load some file and process it. Im sure you can think of other usages.

Wget page from server using IP

Sometimes you have internal IP but you want to get page from the virtual host. You can do that using wget passing host header like this:

wget --header="Host: whatever.domain.name.com" http://127.0.0.1/some/path/file.php

This way you connect to the server using IP but request page from certain path and domain name.

Comments

Post new comment

Image CAPTCHA

About the author

Artur Ejsmont

Hi, my name is Artur Ejsmont,
welcome to my blog.

I am a passionate software engineer living in Sydney and working for Yahoo! Drop me a line or leave a comment.

Enjoy!