[Go]php Curl Set host curl_setopt Curlopt_httpheader specify host

Source: Internet
Author: User

From:http://digdeeply.org/archives/10132139.html

When we develop tests, sometimes the Web server binds a domain name, but because DNS is unresolved, we need to set the host file to access it.
However, if we are required to access through curl, we cannot access the URL's host hosts. Therefore, it needs to be accessed by specifying the host, with the following specific methods:
If it's a Curl command under Linux:

Example
1 curl --silent -H "Host: www.digdeeply.info""192.168.0.1/index.php"

If you use PHP's curl, use curl_setopt to set up Curlopt_httpheader.
Please refer to the following function for use:

Example
123456789101112131415161718192021 //httpHeader    设置的 http head 参数 数组形式 如 array(‘Host: digdeeply.info‘)function curl_by_host($url,$postString=‘‘,$httpHeader=‘‘){    $ch = curl_init();    curl_setopt($ch,CURLOPT_URL,$url);    curl_setopt($ch,CURLOPT_POSTFIELDS,$postString);    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);    curl_setopt($ch,CURLOPT_USERAGENT,$_SERVER[‘HTTP_USER_AGENT‘]);    if(!empty($httpHeader) && is_array($httpHeader))    {        curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeader);    }    $data = curl_exec($ch);    $info = curl_getinfo($ch);    curl_close($ch);    if(curl_errno($ch)){        return $info;    }    return $data;}

[Go]php Curl Set host curl_setopt Curlopt_httpheader specify host

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.