简体   繁体   中英

curl --resolve equivalent in PHP CURL library

Is there an equivalent for curl --resolve .... in PHP CURL library ?

Background: I have round-robin DNS (one domain name resolves to multiple IPs), and I want to send a request to a specific host. I use apache name-based virtual hosts, and thus a correct domain name must appear in the HTTP request.

I have tried specifying the IP in the request URL: curl_setopt($ch, CURLOPT_URL, '127.0.0.1') , and using curl_setopt($ch, CURLOPT_HTTPHEADER, 'Host: example.com') . It works for HTTP, but for HTTPS I get SSL verification error (apparently CURL verifies certificate vs. URL hostname and NOT Host: hostname).

Using hosts file is not a convenient option.

First, the best way to get an answer to how any curl option translates to libcurl (or PHP/CURL) is by using the --libcurl option.

If you do, you will learn that --resolve translates to CURLOPT_RESOLVE with libcurl, supported in PHP since 5.5.0 . Supported by libcurl since 7.21.3 .

I have a solution to this with the help of Python.

In the .php file, just echo the output of python:

<?php
echo passthru("python python.py");
?>

In python.py:

import httplib
conn = httplib.HTTPSConnection('127.0.0.1');
url = "example.com"
conn.request("GET", url)
content = conn.getresponse()
print content.read()
conn.close()

如果你打算分叉另一个进程,只需使用“--resolve”选项与另一个脚本进行分叉。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM