Jump to content

Creating variables from JSON result


aquiss

Recommended Posts

I'm trying to get the JSON array result for serviceids and turn it into a useable variable, but my limited coding ability I seem to be missing something.

How do I do this?

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'snipped');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
    http_build_query(
        array(
            'action' => 'AddOrder',
            'username' => 'snipped',
            'password' => 'snipped',
            'clientid' => "1",
            'pid' => "97",
            'billingcycle' => 'monthly',
            'paymentmethod' => 'stripe',
            'noinvoice' => 'true',
            'noinvoiceemail' => 'true',
            'noemail' => 'true',
            'noinvoice' => 'true',
            'responsetype' => 'json',
        )
    )
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);

// Decode response
$jsonData = json_decode($response, true);


$serviceid = $jsonData[0]["serviceids"];
?>

 

Link to comment
Share on other sites

So I have resolved this myself. I was almost there, but for anyone reading and needing some basic code to get them going (because WHMCS own docs seem to lack the final bit), the following will perform the action and then you can get the variables out.

You will see in the finished code the likes of clientid and pid have " " (double quotes) around them, rather than ' (single quotes).  This is so you can pass a variable into your array from elsewhere.

            'clientid' => "$clientid",
            'pid' => "$pid",

 

Full basic code example

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'your url to api');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
    http_build_query(
        array(
            'action' => 'AddOrder',
            'username' => 'your api username',
            'password' => 'your api password',
            'clientid' => "1",
            'pid' => "97",
            'billingcycle' => 'monthly',
            'paymentmethod' => 'stripe',
            'noinvoice' => 'true',
            'noinvoiceemail' => 'true',
            'noemail' => 'true',
            'noinvoice' => 'true',
            'responsetype' => 'json',
        )
    )
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);

// Decode response
$jsonData = json_decode($response, true);

?>

<?php
$serviceids = $jsonData['serviceids'];
?>

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated