PhoneGap API Curl from Command Line to php

2019-08-07 19:11发布

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;

2条回答
虎瘦雄心在
2楼-- · 2019-08-07 19:38

I have found the solution.

There is some problem on my localhost settings. When I uploaded the code on web server, it started working.

查看更多
何必那么认真
3楼-- · 2019-08-07 20:02

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);
查看更多
登录 后发表回答