CURL请求在PHP中不起作用(但它在命令行中工作)

时间:2017-07-13 14:42:02

标签: php curl request resolve

curl请求在命令行中起作用,但不能与php一起使用。我知道它也适用于node.js.

我也知道我必须和服务在同一个国家。目前服务返回我不在正确的国家,但我不明白为什么。 CURLOPT_RESOLVE似乎是个问题。

$params = array(
'origin' => 'Strasbourg',
'destination' => 'Paris (Toutes gares intramuros)',
'directTravel' => false,
'asymmetrical' => false,
'professional' => false,
'customerAccount' => false,
'oneWayTravel' => true,
'departureDate' => '2017-07-26T00:00:00',
'returnDate' => null,
'travelClass' => 'SECOND',
'passengers' => array(
    'travelerId' => null,
    'profile' => 'adult',
    'age' => 26,
    'fidelityCardType' => 'NONE',
    'fidelityCardNumber' => null,
    'commercialCardType' => 'NONE'
),
'withRecliningSeat' => false,
'physicalSpace' => null);

$resolve = array(sprintf(
   "%s:%d:%s", 
   '<the url without https://>',
   443,
   '127.0.0.1'
));

$headers[] = 'Accept:application/json, text/plain, */*';
$headers[] = 'Content-Type:application/json;charset=UTF-8';
$headers[] = 'Content-length: 0';

$url = "<the url in https>";
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; MSIE 9.0; WIndows NT 9.0; en-US))');
curl_setopt($ch, CURLOPT_RESOLVE, $resolve);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params)); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$output = curl_exec ($ch); 

curl_close ($ch);

命令行curl:

curl -i -k -X POST -A "Mozilla/5.0 (Windows; U; MSIE 9.0; WIndows NT 9.0; en-US))" -H "Accept:application/json, text/plain, */*" --resolve "<the url without https://>:443:127.0.0.1" -H "Content-Type:application/json;charset=UTF-8" -d '{"origin":"Strasbourg","destination":"Paris (Toutes gares intramuros)","directTravel":false,"asymmetrical":false,"professional":false,"customerAccount":false,"oneWayTravel":true,"departureDate":"2017-07-26T00:00:00","returnDate":null,"travelClass":"SECOND","passengers":[{"travelerId":null,"profile":"ADULT","age":26,"fidelityCardType":"NONE","fidelityCardNumber":null,"commercialCardType":"NONE"}],"withRecliningSeat":false,"physicalSpace":null}' '<the url in https>'

我收到了CURLOPT_RESOLVE选项的错误:

Warning: curl_setopt() [function.curl-setopt]: Invalid curl configuration option

有什么想法吗?

0 个答案:

没有答案