PhoneGap Build API : CURL Command Line to PHP - php

I am working on PhoneGap Build API to upload file and create its apk. You can check it here https://build.phonegap.com/docs/write_api
I am facing problem in implementing curl using php. The following curl statement on command line is working fine.
$ curl -F file=#/Users/alunny/index.html -u andrew.lunny#nitobi.com -F 'data={"title":"API V1 App","package":"com.alunny.apiv1","version":"0.1.0","create_method":"file"}' https://build.phonegap.com/api/v1/apps
I am getting errors when I try to write this code using php. I have written the following code.
$username = "email";
$password = "password";
$target_url = "https://build.phonegap.com/api/v1/apps";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $target_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
$post = array(
"file"=>"#www.zip",
"data"=>json_encode(array("title"=>"My Test App","package"=>"com.alunny.apiv1","version"=>"0.1.0","create_method"=>"file"))
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$result = curl_exec($ch);
curl_close ($ch);
By running this code, I am getting this error "no create_method specified: file, remote_repo, or hosted_repo "
Please help me solving this issue.
Thank you

Try shell_exec() function:
$cmd = "https://build.phonegap.com/api/v1/apps";
$out = shell_exec($cmd);
$data = json_decode($out);

Related

php curl ubuntu 400 bad request

i have a working curl script and was testing it at my windows pc with xampp
<?php
$username = '123';
$password = '123456';
$url = 'https://192.168.0.100/test';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
$exec = curl_exec($ch);
curl_close($ch);
echo $exec;
?>
Now I want to transfer it to my raspberry pi but i get an 400 error.
Whats the difference with the code ?
Can you help me ?
Thanks

Converting a Curl command into PHP Curl code (file transfer & custom command)

I am trying to convert the following Curl command:
curl --digest --user "username:password" --verbose --url "http://127.0.0.1/ws?graph-uri=http://localhost/dataset/import/" -X POST -T /data/datasets/foo.n3
Here is the code that appears to be ok, but that doesn't work:
$args = array();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://127.0.0.1/ws?graph-uri=http://localhost/dataset/import/');
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($ch, CURLOPT_USERPWD, "username:password");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
$args['graph-uri'] = curl_file_create('/data/datasets/foo.n3');
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$result = curl_exec($ch);
It seems that the file is actually not uploaded with the PHP code, but everywhere I look the usage of curl_file_create() seems good.
Try this previous answer, substituting graph-uri for file_contents. Also, you are setting "graph-uri" as both GET and POST params, which could collide, and the graph-uri GET param is not URL encoded, which could behave differently in the shell vs. PHP. So you might try the following:
http://127.0.0.1/ws?graph-uri=http%3A%2F%2Flocalhost%2Fdataset%2Fimport%2F

php: curl script working but not in php file

I have a curl script that is working fine in terminal.
curl -XPOST \
-F "file=#var/www/myfile.wav;type=audio/wav" \
-H 'Authorization: Bearer $MY_TOKEN' \
'https://myurl'
I have tried this way in my php file,
<?php
$url = "https://myurl";
$key = "myKey";
$path = "/var/www/";
$fileName = "myfile.wav";
$data['file']= "#".$path.$fileName;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,array("Authorization: Bearer $key"));
$response = curl_exec($ch);
curl_close($ch);
echo $response;
?>
I get an error from the server. Seems I am not making request as the server expects. Am I missing anything in the php script?
Try this:
$data = array('file' => file_get_contents($path.$fileName));
There may be a problem with your CURL library that PHP is using.
Or PHP may be blocked from making HTTPS connections.
Try a series of simple tests using CURL in PHP like this first one with POST false:
<?php
$url = "https://myurl/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
header("Content-Type: text/plain");
echo $response;
?>
and then with POST true...
<?php
$url = "https://myurl/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
header("Content-Type: text/plain");
echo $response;
?>
Since it's an HTTPs URL , You need to make use of this parameter
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

PhoneGap API Curl from Command Line to php

I want to build phonegap application using the API give on https://build.phonegap.com/docs/api
It requires the following curl command to run which will return a token.
$ curl -u andrew.lunny#nitobi.com -X POST "" https://build.phonegap.com/token
This curl statement is working for me on command line. I want to use it in php. I tried the php code given below but its not working.
$username = "USERNAME#gmail.com";
$password = "PASSWORD";
$target_url = "https://build.phonegap.com/token"
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $target_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, '');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$result = curl_exec($ch);
curl_close ($ch);
echo $result;
You can also try the shell_exec() function:
$cmd = "curl -u andrew.lunny#nitobi.com -X POST "" https://build.phonegap.com/token";
$out = shell_exec($cmd);
$data = json_decode($out);
I have found the solution.
There is some problem on my localhost settings. When I uploaded the code on web server, it started working.

convert curl line command in php code

can anyone help me to convert that curl comand line in a php curl code ? plz
curl -v --data "WSCommunityStringRW?2=1200ve50set&Submit=Submit" http://xxxxx/123 -u "admin:a1s2d3" --anyauth
Yes.I did something but ... not work :(
$api_url = 'xxx/123';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($ch, CURLOPT_USERPWD, "admin:a1s2d3");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
$data = array(
'WSCommunityStringRW?2' => '1200ve50set',
'Submit' => 'Submit'
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_close($ch);
Have you taken a look at the cURL pages in the PHP manual? It should contain everything you need for this.
Edit
In the code you've provided, curl_exec() is missing. If it doesn't work after calling curl_exec(), try looking at the output of curl_error().

Categories