[Python] Python to PHP - Azure Machine Learning

Discussão em 'Python' iniciado por Stack, Junho 20, 2025.

  1. Stack

    Stack Membro Participativo

    Hi im trying to translate this python script to php. I don't have much knowledge of Python and limited for PHP.

    The python script is:

    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()))


    In a bid to try and convert it myself, here is what I have done so far:

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


    im sure I have got lots wrong but would appreciate the help.

    thanks

    Continue reading...

Compartilhe esta Página