2016-05-24 44 views
0

我有一个需要HTTP/Request2.php的API。API HTTP/Request2.php到CURL

(Apache的HTTP客户端的HTTP组件(http://hc.apache.org/httpcomponents-client-ga/

我可以使用卷曲取而代之的,是有什么办法不使用这个组件?

这里是API代码

<?php 
require_once 'HTTP/Request2.php'; 

$request = new Http_Request2('http://ww'); 
$url = $request->getUrl(); 

$headers = array(
    // Request headers 
    'Content-Type' => 'application/json', 
    'Ocp-Apim-Subscription-Key' => '{subscription key}', 
); 

$request->setHeader($headers); 

$parameters = array(
    // Request parameters 
); 

$url->setQueryVariables($parameters); 

$request->setMethod(HTTP_Request2::METHOD_POST); 

// Request body 
$request->setBody("{body}"); 

try 
{ 
    $response = $request->send(); 
    echo $response->getBody(); 
} 
catch (HttpException $ex) 
{ 
    echo $ex; 
} 

?> 
+0

没有足够的信息来给出良好的方向。理论上你应该可以使用cURL来做到这一点,因为在一天结束的时候,你可能正在做的就是形成一个HTTP请求。现在是否重写代码以使用cURL是完全不同的问题。 –

+0

我用代码更新我的问题 – Markos

回答

0

大概在一年前就已经知道这个问题了,我想我应该提出一个答案,因为我提出了同样的问题,并且它也可能发生在其他人身上。

通过给出的代码和Ocp-Apic-Subscription-Key标题来判断,我想你正在尝试与Microsoft's Vision APIDocumentation)通信。以下是我为了通过cURL与API进行通信所使用的内容:

$headers = array(
    // application/json is also a valid content-type 
    // but in my case I had to set it to octet-steam 
    // for I am trying to send a binary image 
    'Content-Type: application/octet-stream', 
    'Ocp-Apim-Subscription-Key: {subscription key}' 
); 
$curl = $curl_init(); 
curl_setopt($curl, CURLOPT_FRESH_CONNECT, true); // don't cache curl request 
curl_setopt($curl, CURLOPT_URL, '{api endpoint}'); 
curl_setopt($curl, CURLOPT_POST, true); // http POST request 
// if content-type is set to application/json, the POSTFIELDS should be: 
// json_encode(array('url' => $body)) 
curl_setopt($curl, CURLOPT_POSTFIELDS, $body); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // return the transfer as a string of the return value 
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); 
// disabling SSL checks may not be needed, in my case though I had to do it 
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); 
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); 

$response = curl_exec($curl); 
$curlError = curl_error($curl); 
curl_close($curl);