2016-05-24 19 views
0

私はHTTP/Request2.phpを必要とするAPIを持っています。API HTTP/Request2.phpからCURL

(ApacheのHTTPクライアントHTTPコンポーネント(http://hc.apache.org/httpcomponents-client-ga/

は、私が代わりにCURLを使用することができますから、このコンポーネントを使用しないようにどのような方法があるのですか?ここでは

は、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
です

この問題を知っていることはほぼ1年前に尋ねられました。私は同じ問題を思いついたので答えを出すべきだと思って、他の人にも起こったかもしれません。

与えられたコードとOcp-Apic-Subscription-Keyヘッダーで判断すると、Microsoft's Vision APIDocumentation)と通信しようとしているようです。 APIを使用してcURLを使用して通信するために私が使用したのは次のとおりです。

$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);