php - 无法将 Azure ML API 与 PHP 集成

标签 php azure curl machine-learning azure-machine-learning-service

我尝试将 Azure ML API 与 PHP 集成,但不幸的是在响应中出现错误。

更新:我已使用请求响应 API 通过 json 响应发送

下面是执行 PHP 脚本时获得的响应:

array(1) { ["error"]=> array(3) { ["code"]=> string(11) "BadArgument" 
    ["message"]=> string(26) "Invalid argument provided." ["details"]=> array(1)
    {[0]=> array(2) { ["code"]=> string(18) "RequestBodyInvalid" ["message"]=>
    string(68) "No request body provided or error in deserializing the request
    body." } } } }

PHP 脚本:

$url = 'URL';
$api_key = 'API';
$data = array(
    'Inputs'=> array(
        'My Experiment Name'=> array(
            "ColumnNames" => [['Column1'],
                              ['Column2'],
                              ['Column3'],
                              ['Column4'],
                              ['Column5'],
                              ['Column6'],
                              ['Column7']],
            "Values" => [ ['Value1'],
                          ['Value2'],
                          ['Value3'],
                          ['Value4'],
                          ['Value5'],
                          ['Value6'],
                          ['Value7']]
            ),
        ),
        'GlobalParameters' => new StdClass(),
    );

$body = json_encode($data);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Bearer '.$api_key, 'Accept: application/json'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

$response  = json_decode(curl_exec($ch), true);
//echo 'Curl error: ' . curl_error($ch);
curl_close($ch);

var_dump ($response);

我已经遵循了几个例子,仍然无法破解它。请告诉我解决方案。

最佳答案

根据错误信息,我认为该问题是由于在没有正确的json body的情况下请求ML REST API引起的。

建议您可以引用文章"Getting started with the Text Analytics APIs to detect sentiment, key phrases, topics and language"将 JSON 中的输入行正确格式化为请求正文,然后重试。

希望有帮助。

如果您可以更新您的问题以指定您使用的 ML REST API,我认为这对于解决问题非常有帮助。

期待您的更新。

关于php - 无法将 Azure ML API 与 PHP 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37519307/

相关文章:

PHP GET 基于 URL ID 的行

java - 服务器返回 HTTP 响应代码 : 400 for URL : HttpURLConnection Error

php - 将 Firebase Cloud Messaging 发送到手机设备

在 catch block 中设置对象属性时 PHPUnit 测试失败

php - 使用 [shortcode] 设置背景图像的解决方案?

php - Sendmail Wamp Php

azure - 如何访问Azure中的数据-从GCP拉取数据

powershell - 使用 FTP 将文件发布到 Azure Web App 时出错

entity-framework - 查询Azure移动应用程序TableController

php - curl 错误 60,SSL 证书问题 : self signed certificate in certificate chain