aquiss Posted January 2, 2020 Share Posted January 2, 2020 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"]; ?> 0 Quote Link to comment Share on other sites More sharing options...
aquiss Posted January 3, 2020 Author Share Posted January 3, 2020 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']; ?> 0 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.