Reputation: 69
Can anyone convert this curl command that works in command line to php code :
$ curl -u [email protected] -X POST -d "" https://build.phonegap.com/token
I tried this code but didnt work :
$target_url = "https://[email protected]:[email protected]/token"
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
$result=curl_exec ($ch);
curl_close ($ch);
echo $result;
When I am executing the above code , I am getting the error :
301 Moved
The document has moved here(link to gmail.com).
But, if i use the command in command line, it is working fine . Where am i wrong ?
Also, please tell me what does that "-X" mean, and how can convert it to php code ?
Thanks
Upvotes: 0
Views: 670
Reputation: 88667
301
is a redirect response code. Add this line:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
...after curl_init()
and before curl_exec()
to have cURL follow the redirect to the correct location.
The -X
option is used to specify the POST
method in your original command string, which you have mirrored with curl_setopt($ch, CURLOPT_POST, 1);
EDIT
Try this code:
$username = "[email protected]";
$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;
Upvotes: 1