php - 实现curl命令行到php,尝试但得到错误 [英] php - implement curl command line to php, tried but getting error

查看:207
本文介绍了php - 实现curl命令行到php,尝试但得到错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以将此命令行中的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); 

... curl_init

-X 选项用于指定 curl_setopt($ ch,CURLOPT_POST,1); $ $ p>

EDIT



请尝试以下代码:

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


Can anyone convert this curl command that works in command line to php code :

$ curl -u username@gmail.com -X POST -d "" https://build.phonegap.com/token

I tried this code but didnt work :

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

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

解决方案

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 = "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;

这篇关于php - 实现curl命令行到php,尝试但得到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆