PHP Curl Host settings to access the specified host

Source: Internet
Author: User

For security, our Web service hosts are often unable to access the Internet. Maintenance, but also through the springboard machine, SSH login to operate.

Sometimes our program needs to access the extranet. For example, you need to call an interface to other programs outside the network. What should I do now?

We can configure the host access via PHP's curlopt_httpheader of the curl function.

In development, I encountered an example of this.

There is an active program that needs to call an interface over Qzone.

The following code example:

$host = Array ("Host:act.qzone.qq.com"); $data = ' user=xxx&qq=xxx&id=xxx&post=xxx '; $url = ' http:// 192.168.1.12/xxx/xxx/api/'; Var_dump ($this->curl_post ($host, $data, $url));/* Submit Request * @param $host array required to configure the domain name array ("Host:act.qzone.qq.com"); * @param $data string required to submit the data ' user=xxx&qq=xxx&id=xxx&post=xxx ' .... * @param $ URL string to be submitted as ' http://192.168.1.12/xxx/xxx/api/'; */function Curl_post ($host, $data, $url) {$ch = Curl_init ()       ;       $res = curl_setopt ($ch, Curlopt_url, $url);       Var_dump ($res);       curl_setopt ($ch, Curlopt_ssl_verifyhost, FALSE);       curl_setopt ($ch, Curlopt_ssl_verifypeer, FALSE);       curl_setopt ($ch, Curlopt_header, 0);       curl_setopt ($ch, Curlopt_post, 1);       curl_setopt ($ch, Curlopt_postfields, $data);       curl_setopt ($ch, Curlopt_returntransfer, 1);       curl_setopt ($ch, Curlopt_httpheader, $host);       $result = curl_exec ($ch);       Curl_close ($ch);       if ($result = = NULL) {return 0;  }     Tmdebugutils::d ebuglog ($result);    return $result; }

Setting host via PHP CURL Curlopt_httpheader is a convenient solution to the problem of accessing the Extranet interface.

PHP Curl Host settings to access the specified 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.