问题描述
我有一个网站用CakePHP编写,需要从Veeva Vault下载文件。我从别人那里继承了这个认证函数,它应该返回一个会话id从Veeva,但它返回false curl_exec(),当它应该返回true。我被告知这个函数使用了CakePHP之外的一个测试文件,所以我认为它与Cake相关的东西。
私有函数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('错误:调用URL $ url失败,状态为''。$ status。' 。$ json_response。'',curl_error'。curl_error($ curl)。',curl_errno'。curl_errno($ curl)。'');
}
else
{
//启用以下行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) 148,curl)。我不知道对不对。
curl_getinfo($ curl,CURLINFO_HTTP_CODE)返回200,所以我知道这很好。
curl_exec($ curl)返回false。
返回的catch:
致命错误
错误:卷曲失败,错误#56:分块编码数据中的问题(2)
文件:C:\wamp\www\content\app\Vendor \veeva\veeva.php
Line:109
注意:如果要自定义此错误消息,请创建app\View\Errors\fatal_error.ctp
我需要在运行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()之前添加以下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));
I have a site written in CakePHP which needs to download files from Veeva Vault. I inherited this authentication function from someone else which is supposed to return a session id from Veeva, but it returns false on curl_exec() when it should return true. I was told the function worked with a test file outside CakePHP so I'm thinking its something Cake related.
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) returns resource(148, curl) . I don't know if that's right or not.
curl_getinfo($curl, CURLINFO_HTTP_CODE) returns 200, so I know that's good.
curl_exec($curl) returns false.
The catch returned:
Fatal ErrorError: Curl failed with error #56: Problem (2) in the Chunked-Encoded data
File: C:\wamp\www\content\app\Vendor\veeva\veeva.php
Line: 109Notice: If you want to customize this error message, create app\View\Errors\fatal_error.ctp
Unfortunately I can't seem to find any helpful Veeva documentation.
SOLUTION
I needed to add the following curl option before running curl_exec(),
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
This returned a proper json response. Though it could not be directly entered into json_decode() and had to be edited as such:
$json_utf8 = utf8_decode($json_response);
$return = json_decode(str_replace("?", "", $json_utf8));
SOLUTION
I needed to add the following curl option before running curl_exec(),
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
This returned a proper json response. Though it could not be directly entered into json_decode() and had to be edited as such:
$json_utf8 = utf8_decode($json_response);
$return = json_decode(str_replace("?", "", $json_utf8));
这篇关于curl_exec不能与Veeva Vault集成到CakePHP中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!