【问题标题】:PHP "Json Format Error" on Azure Cognitive ServicesAzure 认知服务上的 PHP“Json 格式错误”
【发布时间】:2018-01-20 23:56:26
【问题描述】:

我正在尝试使用 Azure OCR 服务来取回图像的文本。 不幸的是,我总是收到此错误:

{"code":"BadArgument","re​​questId":"49cecd3b-7be3-4aaa-9a5e-fXXXXXXXXXXXX","message":"JSON 格式错误。"}

我的代码是:

<?php

(http://hc.apache.org/httpcomponents-client-ga/)
require_once 'HTTP/Request2.php';

$request = new Http_Request2('https://westcentralus.api.cognitive.microsoft.com/vision/v1.0/ocr'); 
$url = $request->getUrl();

$headers = array(

    'Content-Type' => 'application/json',

    'Ocp-Apim-Subscription-Key' => 'c7b9e7ab25b14XXXXXXXXXXXXXXXXXX',
);


$request->setHeader($headers);

$parameters = array(

    'language' => 'unk',
    'detectOrientation ' => 'true',
);

$url->setQueryVariables($parameters);

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


$request->setBody("https://image.spreadshirtmedia.net/image-server/v1/mp/compositions/P116103877MPC131734766/views/1,width=300,height=300,appearanceId=1,backgroundColor=E8E8E8,version=1472099537/hallo-sprechblase-auf-babybauch-oder-babyshirt-t-shirts-maenner-premium-t-shirt.jpg");  

try
{
    $response = $request->send();
    echo $response->getBody();  

}
catch (HttpException $ex)
{
    echo "Fehler :(";
    echo $ex;
}
?>

【问题讨论】:

    标签: php json azure ocr microsoft-cognitive


    【解决方案1】:

    您已将图像 URL 指定为正文,但如错误消息所示,它需要编码为 JSON。试试:

    $body = array('url' => 'https://image.spreadshirtmedia.net/image-server/v1/mp/compositions/P116103877MPC131734766/views/1,width=300,height=300,appearanceId=1,backgroundColor=E8E8E8,version=1472099537/hallo-sprechblase-auf-babybauch-oder-babyshirt-t-shirts-maenner-premium-t-shirt.jpg');
    
    $request->setBody(json_encode($body));
    

    【讨论】:

      猜你喜欢
      • 2019-01-07
      • 2019-11-17
      • 1970-01-01
      • 2019-04-12
      • 1970-01-01
      • 2017-02-22
      • 1970-01-01
      • 1970-01-01
      • 2021-07-01
      相关资源
      最近更新 更多