Convert lengthy CURL command to PHP 7 code?

I recently purchased an Onion Omega2+ micro processor, and succeeded at getting it hooked up to their "Onion Cloud", .. where I can supposedly tap the device's API and remotely control it over the internet using CURL commands similar to the following (note that I've excluded my actual API key for obvious reasons):

curl -H "Content-Type: application/json" -H "X-API-KEY: <Your_Api_Key>" -X POST -d '{"command":"aplay","params":["/tmp/mounts/SD-P1/noodle.wav"],"env":""}' https://api.onion.io/v1/devices/c7477c88-f547-4b4e-9782-507e0c2bd386/file/exec

Open in new window


Does anyone here have any idea how I might translate this into PHP code that would run under PHP 7 ?  

Thanks,
- Yvan

P.S. Note that I do NOT have access to the command line on my shared hosting environment were I'd like to test the PHP script.
egoselfaxisAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
No means to test but you can try this
<?php
define('APIKEY', 'YourAPIKeyHere');
$url = "https://api.onion.io/v1/devices/c7477c88-f547-4b4e-9782-507e0c2bd386/file/exec";
$data='{"command":"aplay","params":["/tmp/mounts/SD-P1/noodle.wav"],"env":""}';
curl_setopt($ch, CURLOPT_HTTPHEADER, 
array(
	'Content-Type: application/json',
	'X-API-KEY:' . APIKEY
));
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);      
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);

curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_STDERR, $fp);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$err = curl_error($ch);
if ($err){
	$errno = curl_errno($ch);
	// handle error here
} 
else {
	$response = curl_exec($ch);
	// $response holds returned data
}
curl_close ($ch);

Open in new window

1
egoselfaxisAuthor Commented:
Thanks Julian.  I tried plugging in my API key and running the script up on my server -- and here are are some of the errors I'm getting:

Notice: Undefined variable: ch in /home/yvan/public_html/test.php on line 13
Warning: curl_setopt() expects parameter 1 to be resource, null given in /home/yvan/public_html/test.php on line 15
Notice: Undefined variable: fp in /home/yvan/public_html/test.php on line 26
Warning: curl_close() expects parameter 1 to be resource, null given in /home/yvan/public_html/test.php on line 39

 - Yvan
0
Julian HansenCommented:
sorry missed a line you need this at the start
$ch = curl_init();

Open in new window

Full listing would be
<?php
define('APIKEY', 'YourAPIKeyHere');
$url = "https://api.onion.io/v1/devices/c7477c88-f547-4b4e-9782-507e0c2bd386/file/exec";
$data='{"command":"aplay","params":["/tmp/mounts/SD-P1/noodle.wav"],"env":""}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, 
array(
	'Content-Type: application/json',
	'X-API-KEY:' . APIKEY
));
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);      
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);

curl_setopt($ch, CURLOPT_AUTOREFERER, true);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$err = curl_error($ch);
if ($err){
	$errno = curl_errno($ch);
	// handle error here
	echo $err . ':' . $errno;
} 
else {
	$response = curl_exec($ch);
	// $response holds returned data
	$data = json_decode($response);
	echo "<pre>" . print_r($data, true) . "</pre>";
}
curl_close ($ch);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
egoselfaxisAuthor Commented:
Thanks Julian!  The script is now working (it's triggering the playing of my WAV file on my device).  It's not playing the WAV file in it's entirety, however .. and I'm not sure why.  It's only playing like the first 3 seconds of it for some reason.   I might need to experiment with the params a bit more, as I wasn't getting this behavior when running these API calls via the Onion Cloud interface.  Oh well -- thanks again for your help!

- Yvan
0
Julian HansenCommented:
You are welcome.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.