简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM