PhoneGap API Curl from Command Line to php - 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.

Related

PHP Curl with flag for username/password

Im trying to make a simple curl request to the desk.com api using PHP but having trouble getting a response. Below is their documentation.
$ curl https://yoursite.desk.com/api/v2/cases/:id \
-u email:password \
-H 'Accept: application/json'
Here is my PHP to try and accomplish the above
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://url.desk.com/api/v2/cases -u email:password");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 'Accept: application/json');
$output = curl_exec($ch);
var_dump($output);
curl_close($ch);
Ive barely used curl before so any help as to why I'm not getting a response would be appreciated.
Have you tried sending basic authorization?
curl_setopt($ch, CURLOPT_URL, "https://url.desk.com/api/v2/cases");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HEADER, 'Accept: application/json');
You also won't need the '-u' in your url, that is meant for command line curl.
Additionally, You'll need to replace ':id' with the ID of the case for the above to return a specific case. For example:
curl_setopt($ch, CURLOPT_URL, "https://url.desk.com/api/v2/cases/1");
Try this instead, be sure to define/replace $email and $password
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://url.desk.com/api/v2/cases");
curl_setopt($ch, CURLOPT_USERPWD, "{$email}:{$password}");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 'Accept: application/json');
$output = curl_exec($ch);
var_dump($output);
curl_close($ch);

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);

How can I convert terminal cURL to PHP cURL?

I am using curl in the terminal to access and request a JSON file. It should respond with a JSON file.
curl --basic --user email : password "http:/www.appannie.com/v1/accounts/acntnum/..."
What is its equivalent in PHP curl?
I have used:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $email.".".$password);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$response = curl_exec($ch);
$info = curl_getinfo($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $response;
But it fails.
My reference is this:
http://support.appannie.com/categories/20082753-Analytics-API
I believe the USER PASSWORD needs to be separated by ':'
curl_setopt($ch, CURLOPT_USERPWD, $email.":".$password);

PhoneGap Build API : CURL Command Line to 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);

Categories