从cURL cli语法构造PHP curl语句

时间:2015-08-18 18:45:02

标签: php curl

以下是我正在运行的 cURL 语法(占位符#'代替有效ID' s):

curl -H "Accept: text/xml" https://111222333444555:99999888887777755555@www.sendowl.com/api/v1/products

我收到了有效的xml回复:

<?xml version="1.0" encoding="UTF-8"?><products><product><created-at>2015-08-06T20:51:19Z</created-at>....

以下是我目前的PHP语法:

<?php

//Initialize curl object
$ch = curl_init();

$apikey = "111222333444555";
$apisecret = "99999888887777755555";
$uri = "https://$apikey:$apisecret@www.sendowl.com/api/v1/products";
echo $uri;
//Define curl options in an array
$options = array(CURLOPT_URL => $uri,
                 CURLOPT_HEADER => "Accept: text/xml",
                 CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
                 CURLOPT_RETURNTRANSFER => TRUE
);

//Set options against curl object
curl_setopt_array($ch, $options);

//Assign execution of curl object to a variable
$data = curl_exec($ch);

//Close curl object
curl_close($ch);

echo ("data is: $data");
//Pass results to the SimpleXMLElement function
$xml = new SimpleXMLElement($data);

print_r($xml);

?>

似乎$data始终为空,因此我收到以下错误:

Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in C:\xampp\htdocs\whd.com\license.php:27 Stack trace: #0 C:\xampp\htdocs\whd.com\license.php(27): SimpleXMLElement->__construct('') #1 {main} thrown in C:\xampp\htdocs\whd.com\license.php on line 27

1 个答案:

答案 0 :(得分:0)

我对堆栈溢出不够熟悉,知道这是否是正确的帮助方式,但是:

CURLOPT_HEADER => "Accept: text/xml"

应该是

CURLOPT_HTTPHEADER => array('Accept: text/xml')