我想用curl代替http請求2在PHP中的梨模塊查詢Microsoft Project Oxford api
。他們有一個現成的庫,可以方便地調用他們的API,但它使用了一個名爲http request2的pear模塊。我真的不知道如何在服務器上安裝pear模塊,所以我想只是重寫庫的一些部分來使用curl。下面是我特別希望修改其代碼的一部分:如何將http請求2的梨碼轉換爲curl php
// This sample uses the HTTP_Request2 package. (for more information: http://pear.php.net/package/HTTP_Request2)
require_once 'HTTP/Request2.php';
$headers = array(
'Content-Type' => 'application/json',
);
$query_params = array(
// Specify your subscription key
'subscription-key' => '',
// Specify values for optional parameters, as needed
'visualFeatures' => 'All',
);
$request = new Http_Request2('http://api.projectoxford.ai/vision/v1/ocr?language=unk&detectOrientation =true');
$request->setMethod(HTTP_Request2::METHOD_POST);
// Basic Authorization Sample
// $request-setAuth('{username}', '{password}');
$request->setHeader($headers);
$url = $request->getUrl();
$url->setQueryVariables($query_params);
$request->setBody("{'Url':'https://oxfordportal.blob.core.windows.net/vision/OpticalCharacterRecognition/1.jpg'}");
echo '<pre>';
print_r($request);
echo '</pre>';
try
{
$response = $request->send();
echo '<pre>';
print_r(var_dump(json_decode($response->getBody(), true)));
echo '</pre>';
}
catch (HttpException $ex)
{
echo $ex;
}
而這裏的代碼,我到目前爲止有:
$headers = array(
'Content-Type' => 'application/json',
'subscription-key' => ''
);
/*
$query_params = array(
array('Url'=>'https://oxfordportal.blob.core.windows.net/vision/OpticalCharacterRecognition/1.jpg')
);
*/
$query_params = '{ "Url": "https://oxfordportal.blob.core.windows.net/vision/OpticalCharacterRecognition/1.jpg"}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://api.projectoxford.ai/vision/v1/ocr?language=en&detectOrientation =true');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query_params); //Post Fields
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$ret_arr = curl_exec ($ch);
echo curl_error($ch);
curl_close ($ch);
但它給我的HTTP格式錯誤請求錯誤400。 ..我的捲髮代碼有什麼問題?請幫助......