2015-04-04 62 views
2

嗨即时尝试将此Python脚本转换为PHP。我对Python没有太多的知识,并且仅限于PHP。Python到PHP - Azure机器学习

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

我敢肯定,我有许多错误的,但将不胜感激的帮助。

感谢

+0

对我来说看起来很不错,减去你没有在相同程度上显示错误的事实 - 你有特定的问题吗? – jedwards 2015-04-04 20:20:57

+0

还没有在这个问题上看到一个问题,应该在代码审查网站? – 2015-04-04 20:55:33

回答

1

我只是有这个做我自己,但我会为你提供答案。如果你要谈论使用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: [] 

这是一个微妙的区别,但足以让天青抛出一个hissy适合。

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

0

我在修改这个方面遇到了一些麻烦,我希望能够轻松使用JSON & Guzzle。以下是我建立的解决方案。

首先是实际调用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做你所需要的。