curl --resolve equivalent in PHP CURL library

2019-04-26 12:42发布

问题:

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.

回答1:

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.



回答2:

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()


回答3:

if you are going to bother to fork another process, just fork curl itself with the "--resolve" option versus yet another script.



标签: php http ssl curl