cURL从Veeva下载损坏的文件

时间:2014-02-10 03:23:19

标签: php cakephp curl

我有一个从Veeva Vault下载文件的功能。文件大小合适,所以我知道至少我抓住了我应该的东西,但是它们已经损坏而且无法打开。我可以手动从Veeva下载它们并打开它们,所以我知道文件本身不是问题。

public function GetDocumentByUrl($url, $filename)
{
    $this->Auth(); //sets $this->sessID

    $urldata = explode("/", $url);

    $count = 0;
    foreach($urldata as $key => $value)
    {
      if( $value == "documents")
      {
          $count++;
          $id = $urldata[$count];
          break;
      }
      else
      {
          $count++;
      }
    }

    $type = $this->GetDocumentInfo($id, self::VVFILEEXT);

    $fullFilename = $filename . "." . $type;

    set_time_limit(0);
    $fp = fopen (dirname(__FILE__) . '/' . $fullFilename, 'w+');

    $ch = curl_init($url);

    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    curl_setopt($ch, CURLOPT_TIMEOUT, 50);
    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/json;charset=UTF-8", "Authorization: ".$this->sessID));
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    $json_response = curl_exec($ch);
    $status = curl_getinfo($ch, 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). '"');
    }
    curl_close($ch);
    fclose($fp);
}

我尝试过添加CURLOPT_BINARYTRANSFER和CURLOPT_RETURNTRANSFER,但只返回了1kb的损坏文件。

我也尝试将CURLOPT_HTTPHEADER中的内容类型设置为“application / pdf”和“application / octet-stream”,但都没有帮助。

以下是Veeva API文档:http://developer.veevavault.com/?page_id=951#Retrieve_Document_File

0 个答案:

没有答案