Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set hostname using php curl for a specific ip

Tags:

php

curl

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

like image 690
Vishesh Joshi Avatar asked Mar 29 '12 20:03

Vishesh Joshi


People also ask

How cURL URL in PHP?

php file with the following contents. $url = 'https://www.example.com' ; $curl = curl_init(); curl_setopt( $curl , CURLOPT_URL, $url );

Does cURL send a host header?

curl will also make cookies work for example.com in this case, but it will fail miserably if the page redirects to another host and you enable redirect-following (--location ) since curl will send the fake Host: header in all further requests too.

Can I use cURL in PHP?

cURL is a PHP extension that allows you to use the URL syntax to receive and submit data. cURL makes it simple to connect between various websites and domains.


1 Answers

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); 
like image 131
simpleigh Avatar answered Oct 12 '22 19:10

simpleigh