【发布时间】:2015-09-15 19:06:14
【问题描述】:
我正在与 sentione api 集成。 我对 guzzle 有问题,guzzle 向我抛出 401 Unauthorized 异常。 我认为发布数据可能有问题,我尝试以另一种方式设置它,但我总是得到相同的结果。
这是我的代码:
$arr = array(
'topicId' => id,
'from' => '2014-01-01 00:00:00.000 CET'
);
$params = json_encode($arr);
$client = new Client('http://sentione.com/api', array(
'request.options' => array(
'headers' => array(
"Accept: application/json",
"X-API-KEY: key",
"Content-type: application/json",
)
)
));
$request = $client->post('statements/search', array('Content-Type' => 'application/json'), $params);
$response = $request->send();
这就是我的 cURL 定义:
$arr = array(
"topicId" => id,
"from" => "2014-01-01 00:00:00.000 CET"
);
$json = json_encode($arr);
$headers = array(
"Accept: application/json",
"X-API-KEY: key",
"Content-type: application/json",
);
$url = "http://sentione.com/api/statements/search";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_VERBOSE, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $json);
$response = curl_exec($curl);
当然效果很好。 任何人都可以帮助我吗?可能我忘记了什么?
【问题讨论】: