Skip to main content
 首页 » 编程设计

php之Python 到 PHP之Azure 机器学习

2024年08月26日24jirigala

您好,我正在尝试将此 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,但我不确定。