【发布时间】:2014-02-05 18:58:31
【问题描述】:
我有一个用 CakePHP 编写的网站,需要从 Veeva Vault 下载文件。我从其他人那里继承了这个身份验证函数,它应该从 Veeva 返回一个会话 id,但是当它应该返回 true 时,它在 curl_exec() 上返回 false。有人告诉我该函数与 CakePHP 之外的测试文件一起使用,所以我认为它与 Cake 相关。
private function Auth()
{
try {
$url = self::VVURL . '/auth?username=' . self::VVUSERNAME . '&password=' . self::VVPASS;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json;charset=UTF-8"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, null);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$json_response = curl_exec($curl);
if ($json_response != true) {
throw new Exception (curl_error($curl), curl_errno($curl));
}
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if($status != 200)
{
die('Error: call to URL $url failed with status "' . $status .'", response "' . $json_response. '", curl_error "' . curl_error($curl) . '", curl_errno "' . curl_errno($curl). '"');
}
else
{
//Enable following line to DEBUG
//print_r($json_response);
}
curl_close($curl);
$return = json_decode($json_response);
foreach($return as $k => $v)
{
if($k == 'sessionId')
$this->sessID = $v;
}
return $this->sessID;
}
catch(Exception $e){
trigger_error(sprintf(
'Curl failed with error #%d: %s',
$e->getCode(), $e->getMessage()),
E_USER_ERROR);
}
}
curl_init($url) 返回 resource(148, curl) 。不知道对不对。
curl_getinfo($curl, CURLINFO_HTTP_CODE) 返回 200,所以我知道这很好。
curl_exec($curl) 返回 false。
渔获归还:
致命错误
错误:Curl 失败并出现错误 #56:块编码数据中的问题 (2)
文件:C:\wamp\www\content\app\Vendor\veeva\veeva.php
线路:109
注意:如果要自定义此错误信息,请创建 app\View\Errors\fatal_error.ctp
很遗憾,我似乎找不到任何有用的 Veeva 文档。
解决方案
我需要在运行 curl_exec() 之前添加以下 curl 选项,
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
这返回了正确的 json 响应。虽然不能直接输入到 json_decode() 中,只能这样编辑:
$json_utf8 = utf8_decode($json_response);
$return = json_decode(str_replace("?", "", $json_utf8));
【问题讨论】:
-
问题:函数 curl_exec 可能被禁用。该怎么办?解决方案:为了消除此错误消息,您需要执行以下操作之一: 从 php.ini* 文件的 disable_functions 中删除 curl_exec 字符串 如果您无权访问,请让您的托管服务提供商删除上述字符串php.ini* 文件更改允许运行 curl_exec 函数的托管服务提供商。