Python到PHP-Azure机器学习


Python to PHP - Azure Machine Learning

嗨,我正在尝试将这个python脚本翻译成php。我对Python的了解不多,对PHP的了解有限。

python脚本是:

import urllib2
import json 
data =  {
    "Inputs": {
         "input1": {
             "ColumnNames": ["Client_ID"],
             "Values": [ [ "0" ], [ "0" ], ]
         },
     },
     "GlobalParameters": {}
}
body = str.encode(json.dumps(data))
url = 'https://ussouthcentral.services.azureml.net/workspaces/3e1515433b9d477f8bd02b659428cddc/services/cb1b14b17422435984943d51b5957ec7/execute?api-version=2.0&details=true'
api_key = 'abc123'
headers = {'Content-Type':'application/json', 'Authorization':('Bearer '+ api_key)}
req = urllib2.Request(url, body, headers) 
try:
    response = urllib2.urlopen(req)
    result = response.read()
    print(result) 
except urllib2.HTTPError, error:
    print("The request failed with status code: " + str(error.code))
    print(error.info())
    print(json.loads(error.read()))                 

为了尝试自己转换它,以下是我迄今为止所做的:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$data = array(
    'Inputs'=> array(
        'input1'=> array(
            'ColumnNames' => ["Client_ID"],
            'Values' => [ [ "0" ], [ "0" ], ]
        ),
    ),
    'GlobalParameters'=> array()
);
$body = json_encode($data);
$url = 'https://ussouthcentral.services.azureml.net/workspaces/3e1515433b9d477f8bd02b659428cddc/services/cb1b14d17425435984943d41a5957ec7/execute?api-version=2.0&details=true';
$api_key = 'abc123'; 
$headers = array('Content-Type'=>'application/json', 'Authorization'=>('Bearer '+ $api_key));

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $body);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($curl);
var_dump($result);

我肯定我做错了很多事,但我很感激你的帮助。

感谢

我只能自己做这件事,尽管我会为你提供答案。如果你要使用php与Azure的ML平台对话,你需要像这样构建你的CURL调用:

$data = array(
  'Inputs'=> array(
      'input1'=> array(
          'ColumnNames' => array("header1", "header2", "header3"),
          'Values' => array( array("value1" ,  "value2" ,  "value3"))
      ),
  ),
  'GlobalParameters'=> null
);
$body = json_encode($data);
$url = 'your-endpoint-url';
$api_key = 'your-api-key'; 
$headers = array('Content-Type: application/json', 'Authorization:Bearer ' . $api_key, 'Content-Length: ' . strlen($body));
$this->responseArray['body'] = $body;

$curl = curl_init($url); 
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS, $body);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($curl);

在所有挂断电话的地方中,它对我来说是GlobalParameters上的,对你来说也是。你需要这个:

GlobalParameters => null

这将生成以下JSON

GlobalParameters: {}

GlobalParameters => array()

给出

GlobalParameters: []

这是一个微妙的区别,但足以让Azure发出嘶嘶声。

我没有使用您的curl_setopt函数进行测试,而是使用了示例中包含的内容。我假设它可以使用您现有的curl_setopt,但我不确定。

我很难完美地适应它,我希望能够轻松地使用JSON&狂饮下面是我构建的解决方案。

首先是对Azure进行实际调用的函数。请注意,Guzzle希望您的URL分为域和URI两部分。

这些都应该在您的.env文件中(如果您至少使用Laravel)。AZURE_BASE=https://ussouthcentral.services.azureml.net

AZURE_URL=/workspaces/[[YOUR_STUFF_HERE]]/services/[[YOUR_STUFF_HERE]]/execute?api-version=2.0&format=swagger

AZURE_PRIMARY_KEY=[[YOUR_KEY]]

use GuzzleHttp'Client;
public function learn () {
    $client = new Client([
        'base_uri' => env('AZURE_BASE'),
        'timeout'  => 2.0,
    ]);
    $headers = [
        'Authorization' => 'Bearer ' .env('AZURE_PRIMARY_KEY'),        
        'Accept'        => 'application/json',
        'Content-Type' => 'application/json'
    ];
    $data = $this->test_data();
    $body = json_encode($data);
    $response = $client->request('POST', env('AZURE_URL'), [
        'headers' => $headers,
        'body' => $body
    ]);

    return $response;
}

正如其他答案所指出的,数据设置非常敏感。new 'stdClass是这里的关键,因为我们最终需要一个JSON对象({}),而不是一个数组([])。stdClass为我们创建了那个空对象。

function test_data () {
    return array(
        'Inputs'=> array(
            'input1'=> array(
                [
                'DESC' => "",   
                '2-week-total'=> "1",   
                'last-week'=> "1",   
                'this-week'=> "1",   
                'delta'=> "1",   
                'normalized delta'=> "1",   
                'delta-percent'=> "1",   
                'high-total-delta'=> "1",   
                'high-total-amt'=> "1",   
                'role'=> ""
                ]
            ),
        ),
        'GlobalParameters'=> new 'stdClass,
    );
}

现在,当您调用->learn()时,您将返回一些不错的JSON来做您需要的事情。