2015-04-05 61 views
0

我试着去得到这个API在PHPAzure的API - PHP请求

API文档的工作是在这里

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

我真的卡住和IM如此接近得到它的工作。以下是我目前的代码,如果有人可以发现任何错误。我还包含了我的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); 

IM仍然curl_error没有得到错误和VAR转储只是说布尔(假)

回答

4

您对元素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); 
1

你必须curl_exec()后运行curl_error(),因为curl_error()不会返回包含当前会话中的最后一个错误的字符串。 (source : php.net)

所以走这条路

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

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

+0

谢谢!这有帮助!现在我得到了字符串(194)“{”error“:{”code“:”BadArgument“,”message“:”提供的参数无效“,”details“:[{”code“:”RequestBodyInvalid“ :“请求主体没有提供或反序列化请求主体时出错”}}}}“ – user2229747 2015-04-05 13:13:36