note 102793 modified in function.curl-setopt by danbrown
| From: | danbrown@php.net | Date: | Mon, 07 Mar 2011 12:33:26 +0000 |
| Subject: | note 102793 modified in function.curl-setopt by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-177615@lists.php.net to get a copy of this message | ||
Force Curl Request To Go To A Particular IP Address
Yes, there is a method of passing an IP address to curl. Excellent for services with multiple IP
addresses and also to take DNS out of the equation for testing/debugging.
<?php
function fetch_page($url, $host_ip = NULL)
{
$ch = curl_init();
if (!is_null($host_ip))
{
$urldata = parse_url($url);
// Ensure we have the query too, if there is any...
if (!empty($urldata['query']))
$urldata['path'] .= "?".$urldata['query'];
// Specify the host (name) we want to fetch...
$headers = array("Host: ".$urldata['host']);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// create the connecting url (with the hostname replaced by IP)
$url = $urldata['scheme']."://".$host_ip.$urldata['path'];
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec ($ch);
curl_close ($ch);
return $result;
}
?>
--was--
Force Curl Request To Go To A Particular IP Address
Yes, there is a method of passing an IP address to curl. Excellent for services with multiple IP
addresses and also to take DNS out of the equation for testing/debugging.
function fetch_page($url, $host_ip = NULL)
{
$ch = curl_init();
if (!is_null($host_ip))
{
$urldata = parse_url($url);
// Ensure we have the query too, if there is any...
if (!empty($urldata['query']))
$urldata['path'] .= "?".$urldata['query'];
// Specify the host (name) we want to fetch...
$headers = array("Host: ".$urldata['host']);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// create the connecting url (with the hostname replaced by IP)
$url = $urldata['scheme']."://".$host_ip.$urldata['path'];
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec ($ch);
curl_close ($ch);
return $result;
}
http://php.net/manual/en/function.curl-setopt.php