2016-02-11 86 views
0

我在我的REST API(使用SLIM框架)中創建了一個測試函數,用於測試雲轉換API的包裝類的實現。在我的REST API中調用cloudconvert API

$app->get('/test', 'authenticate', function() use ($app) { 

    $response = array(); 
    $converter = new CloudConverter(); 
    $url = $converter->createProcess("docx","pdf"); 
    $response["url"] = $url; 
    echoRespnse(201, $response);    

}); 

CloudConverter類裏面我CreateProcess函數看起來像這樣:

public function createProcess($input_format,$output_format) 
{ 
    $this->log->LogInfo("CreateProcess Called"); 

    $headers = array('Content-type: application/json'); 
    $curl_post_data = array('apikey' => API_KEY,'inputformat' => $input_format,'outputformat' => $output_format);   
    $curl = curl_init(CLOUD_CONVERT_HTTP); 
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); 
    curl_setopt($curl, CURLOPT_POST, true); 
    curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($curl_post_data)); 
    $curl_response = curl_exec($curl); 

    if ($curl_response === false) 
    { 
     $info = curl_getinfo($curl); 
     curl_close($curl); 
     die('error occured during curl exec. Additioanl info: ' . var_export($info)); 
     $this->log->LogInfo('error occured during curl exec. Additioanl info: ' . var_export($info)); 
    } 

    curl_close($curl); 
    $decoded = json_decode($curl_response,true); 
    return $decoded['url']; 
} 

我已經使用Chrome高級REST客戶端測試了我的API,我看到我的電話給cloudconvert API的成功響應,但這不是我期待的,正如在上面的代碼中可以看到的那樣。我期待提取網址並在我的回覆中返回THAT。

我的問題是: 我怎樣才能從cloudconvert響應提取URL並返回,在我自己的反應。

回答

1

您需要使用

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true) 

返回響應作爲一個字符串:curl docs

+0

非常感謝。我只是通過curl_setopt選項閱讀,但你擊敗了我:)再次感謝.. .. – DTH