user2229747
user2229747

Reputation: 321

Azure API - PHP Request

Im trying to get this API working in PHP

The API documentation is here

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

Im really stuck and im so close to getting it to work. Below is my current code if anyone can spot any errors. I have also included my API key as it will be changed once working.

<?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 still getting no error from curl_error and the var dump just says bool(false)

Upvotes: 0

Views: 914

Answers (2)

alfallouji
alfallouji

Reputation: 1170

You had an issue with the element GlobalParameters, declare it as a StdClass instead of an empty array. Try this :

<?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);

Upvotes: 4

β.εηοιτ.βε
β.εηοιτ.βε

Reputation: 39304

You have to run curl_error() after curl_exec() because curl_error() does return a string containing the last error for the current session. (source : php.net)

So go this way

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

And you should have a error telling you what is wrong.

Upvotes: 1

Related Questions