How to set hostname using php curl for a specific ip
Asked Answered
B

4

46

Hi I have a server which has several virtual hosts set up on it.

I wanted to make a curl request to this server's ip using php. Also I wanted to make this request to a specific hostname on the server's ip.

Is there a way to do it?

A bit more elaboration : I want to make a curl requests between my servers using internal LAN, using their internal IP. The issue is that I have several sites hosted on this server. So when i make a curl request to the internal IP of the server.. something like (curl_init(xxx.xxx.xxx.xxx)), I want to be able to be tell apache to go to a particular folder pointed to by a virtual host. I hope that made the question a bit more clear.. – Vishesh Joshi 3 mins ago edit

Bobolink answered 29/3, 2012 at 20:3 Comment(4)
You want to show your ip different with curl ?Venn
If it is a hostname being hosted by a webserver then you can simple use "the_hostname.domain" and it should work unless it is a local hostname and youre from outside the network then you would have to make that domain the default domain on that ip so you can use the ip instead. But im not very clear if thats what youre looking atLeptophyllous
I want to make a curl requests between my servers using internal LAN, using their internal IP. The issue is that I have several sites hosted on this server. So when i make a curl request to the internal IP of the server.. something like (curl_init(xxx.xxx.xxx.xxx)), I want to be able to be tell apache to go to a particular folder pointed to by a virtual host. I hope that made the question a bit more clear..Bobolink
Commandline Curl: https://mcmap.net/q/165510/-set-curl-to-use-local-virtual-hosts/367456Autocephalous
R
62

You can set the host header in the curl request:

<?php
$ch = curl_init('XXX.XXX.XXX.XXX');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: subdomain.hostname.com'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
echo curl_exec($ch);
Rosabelle answered 29/3, 2012 at 20:21 Comment(2)
Thanks Leigh. I just did this and it worked. I was just going to post the same answer :) thanks.Bobolink
This didn't work for me because the SSL certificate presented did not match. CURLOPT_RESOLVE below works - but for things like Let's Encrypt, if it's not in DNS, renewing it may be an issue.Labrum
C
35

For HTTPS sites use CURLOPT_RESOLVE which exists in every PHP version since PHP 5.5.

<?php
$ch = curl_init('https://www.example.com/');
// note: array used here
curl_setopt($ch, CURLOPT_RESOLVE, array(
    "www.example.com:443:172.16.1.1",
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$result = curl_exec($ch);

Sample output:

* Added www.example.com:443:172.16.1.1 to DNS cache
* Hostname www.example.com was found in DNS cache
*   Trying 172.16.1.1...
Clamor answered 27/10, 2015 at 1:9 Comment(2)
Note that while this feature is implemented in PHP 5.5, it is still not documented in the PHP documentation. However, this is the correct and only solution in order for ssl certificates to work. For now, additional documentation can be read in CURL manual at: curl.haxx.se/libcurl/c/CURLOPT_RESOLVE.htmlVano
You are awesome! Many thanks! #47941230Tetrahedron
P
14

Base on Leigh Simpson, It works, but I need query string attach with it. That's what I work around:

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://xxx.xxx.xxx.xxx/index.php?page=api&action=getdifficulty");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: subdomain.hostname.com'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
echo curl_exec($ch);
?>
Plastid answered 18/2, 2014 at 19:42 Comment(1)
you need a path in the url: just the host IP is not sufficient. Therefore, you should set curl_setopt($ch, CURLOPT_URL, "xxx.xxx.xxx.xxx/");Horner
S
0

if using https protocol make sure to disable certificate verification, set CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST to false

$headers = ['Host: hostname.com', 'User-Agent: myUA'];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://xxx.xxx.xxx.xxx/index.php");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
Shalon answered 3/4 at 21:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.