Azure API - PHP Request


Azure API - PHP Request

我试图让这个API在PHP 中工作

API文件在这里

https://studio.azureml.net/apihelp/workspaces/3e1515433b9d477f8bd02b659428cddc/webservices/aca8dc0fd2974e7d849bbac9e7675fda/endpoints/cb1b14b17422435984943d41a5957ec7/score

我真的被卡住了,而且我离工作很近。如果有人能发现任何错误,下面是我当前的代码。我还包含了我的API密钥,因为它一旦工作就会更改。

<?php
 error_reporting(E_ALL);
 ini_set('display_errors', 1);
$url = 'https://ussouthcentral.services.azureml.net/workspaces/3e1515433b9d477f8bd02b659428cddc/services/cb1b14b17422435984943d41a5957ec7/execute?api-version=2.0&details=true';
 $api_key = '5ve72/xxLuzaexQu7LyRBl1iRdGqAQiQ1ValodnS7DG+F0NzgHkaLyk1J30MXrlWFovzPzlurui/o5jeH7RMiA=='; 

 $data = array(
     'Inputs'=> array(
         'input1'=> array(
             'ColumnNames' => ['Client_ID'],
             'Values' => [ [ '0' ], [ '0' ], ]
         ),
     ),
     'GlobalParameters' => array()
 );
$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);
 echo 'Curl error: ' . curl_error($ch);
 $response  = curl_exec($ch);
curl_close($ch);
 var_dump($response);

我仍然没有从curl_error得到任何错误,var dump只是说bool(false)

元素GlobalParameters出现问题,请将其声明为StdClass,而不是空数组。试试这个:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$url = 'https://ussouthcentral.services.azureml.net/workspaces/3e1515433b9d477f8bd02b659428cddc/services/cb1b14b17422435984943d41a5957ec7/execute?api-version=2.0&details=true';
$api_key = '5ve72/xxLuzaexQu7LyRBl1iRdGqAQiQ1ValodnS7DG+F0NzgHkaLyk1J30MXrlWFovzPzlurui/o5jeH7RMiA=='; 

$data = array(
        'Inputs'=> array(
            'input1'=> array(
                'ColumnNames' => ['Client_ID'],
                'Values' => [ [ '0' ], [ '0' ], ]
                ),
            ),
        '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);
$response  = curl_exec($ch);
echo $body . PHP_EOL . PHP_EOL;
echo 'Curl error: ' . curl_error($ch);
curl_close($ch);
var_dump($response);

您必须在curl_exec()之后运行curl_error(),因为curl_error()确实返回了一个包含当前会话最后一个错误的字符串。(来源:php.net)

所以走这边

$response  = curl_exec($ch);
echo 'Curl error: ' . curl_error($ch);  

你应该有一个错误告诉你什么是错的。