如何将http请求2的梨码转换为curl php
问题描述:
我想用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。 ..我的卷发代码有什么问题?请帮助......
答
尝试这样
<?php
$headers = array(
'Content-Type' => 'application/json',
'subscription-key' => ''
);
$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);
var_dump($ret_arr);
echo curl_error($ch);
curl_close ($ch);
这会返回一个"statusCode": 401, "message": "Access denied due to missing subscription key. Make sure to include subscription key when making requests to an API."
消息给我。所以400错误至少解决了。
您遇到的问题是由于您的CURLOPT_URL
值中存在空格。
答
这工作:
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_URL, 'http://api.projectoxford.ai/vision/v1/ocr?language=unk&detectOrientation=true');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Host: api.projectoxford.ai',
'Ocp-Apim-Subscription-Key: YOUR_API_KEY_HERE',
));
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"Url": "https://oxfordportal.blob.core.windows.net/vision/OpticalCharacterRecognition/1.jpg"}');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec ($ch);
echo "<h3>Request</h3>\r\n";
debug(curl_getinfo($ch));
echo "<h3>Response</h3>\r\n";
debug($response);
echo curl_error($ch);
curl_close ($ch);
function debug(){
echo'<pre>';
foreach(func_get_args()as $arg){
ob_start();
var_dump($arg);
echo htmlentities(ob_get_clean())."</pre><hr>\r\n";
}
}