php - 实现curl命令行到php,尝试但得到错误 [英] php - implement curl command line to php, tried but getting error
问题描述
任何人都可以将此命令行中的curl命令转换为PHP代码:
$ curl -u username @ gmail。 com -X POST -dhttps://build.phonegap.com/token
我试过此代码但无法工作:
$ target_url =https://USERNAME@gmail.com:PASSWORD@build.phonegap。 com / 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;
当我执行上面的代码,我得到的错误:
301移动
文档已移至此处(链接到gmail.com)。
但是,如果我在命令行中使用命令,它工作正常。
此外,请告诉我-X是什么意思,以及如何将其转换为PHP代码?
感谢
301
重定向响应代码。添加此行:
curl_setopt($ ch,CURLOPT_FOLLOWLOCATION,TRUE);
... EDIT 请尝试以下代码: Can anyone convert this curl command that works in command line to php code : I tried this code but didnt work : When I am executing the above code , I am getting the error : 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 ...after The EDIT Try this code:
这篇关于php - 实现curl命令行到php,尝试但得到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! curl_init
-X
选项用于指定 curl_setopt($ ch,CURLOPT_POST,1);
$ $ p>
$ 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;
$ curl -u username@gmail.com -X POST -d "" https://build.phonegap.com/token
$target_url = "https://USERNAME@gmail.com:PASSWORD@build.phonegap.com/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;
301 Moved
The document has moved here(link to gmail.com).
301
is a redirect response code. Add this line:curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_init()
and before curl_exec()
to have cURL follow the redirect to the correct location.-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);
$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;