Converting command-line cURL where URL is piped to python

January 06, 2018, at 08:48 AM

I have this command-line cURL example:

curl -X PUT -H "X-Auth-Token: $AUTH_TOKEN" \
     -d "{\"data\":{\"name\":\"Device1 Callflow\", \"numbers\":[\"1001\"], \"flow\":{\"module\":\"device\",\"data\":{\"id\":\"$DEVICE_ID\"}}}}" \$ACCOUNT_ID/callflows | python -mjson.tool

My task is to convert this into a PHP function, where the needed values are passed into the function:

function someFunc($AUTH_TOKEN, $ACCOUNT_ID, $DEVICE_ID, $numbers) {}

Normally, I use this sort of code:

function setLimits($auth_token, $accountID, $feature1_cnt, $feature2_cnt) {
    $service_url = "$accountID/limits";
    $data = '{"data":{"feature1": ' . $feature1_cnt . ',"feature2": ' . $feature2_cnt . '}}';
    $ch = curl_init($service_url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Auth-Token: $auth_token",'Content-Type: application/json', 'Content-Length: ' . strlen($data)));
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    if ($response === false) {
        $info = curl_getinfo($ch);
        die('error occured during curl exec. Additional info: ' . var_export($info));
    $decoded = json_decode($response);
    if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
        die('error occured: ' . $decoded->response->errormessage);

BUT, for a specific API call, I need to pass this as the service URL:$ACCOUNT_ID/callflows | python -mjson.tool

How do I do that?

Answer 1

First, change:

$data = '{"data":{"feature1": ' . $feature1_cnt . ',"feature2": ' . $feature2_cnt . '}}';


$data = [
    "data" => [
        "feature1" => $feature1_cnt,
        "feature2" => $feature2_cnt

You will make a mistake typing out JSON, and it won't be easy to find or fix.

Second, python -mjson.tool

json.tool [...] to validate and pretty-print:

PHP's JSON validation is pretty much just checking if json_decode() returned NULL, and then you can check json_last_error() and json_last_error_msg() for error info. It is not robust.

You can encode JSON in "pretty-printed" format with:

json_encode($foo, JSON_PRETTY_PRINT);

But I can't think of an earthly reason why you would want to do that other than if there are human eyeballs that are going to be looking at it.

Lastly, in answering your question I've realized that I have no actual idea what you're getting at and you really need to clarify why you think you need to do anything you asked about. [other than the validation, I suppose]

What does this do in PHP/Laravel?

What does this do in PHP/Laravel?

I have been trying to figure out some code in Laravel by reading the docs and stuffI came across a piece of code that I cant quit figure out what it is doing

What is difference between dynamic parameter in oop and $this-> [on hold]

What is difference between dynamic parameter in oop and $this-> [on hold]

i am confused between a dynamic parameter in php like:

Symfony 4 global route prefix

Symfony 4 global route prefix

I can't find any info about the global route prefix in a Symfony 4 applicationThe only thing I've found is annotating the controllers with @route

Old and new password judgement in php

Old and new password judgement in php

Tell me where I am wrongI have written a PHP program in that PHP page I am taking user old and new password