【问题标题】:Sending POST request through PHP using CURL使用 CURL 通过 PHP 发送 POST 请求
【发布时间】:2017-03-24 10:05:51
【问题描述】:

我正在使用 CURL 向 URL 发送 JSON 请求。

这是我的代码:

$uri = "http://xxxxx:3434/config?command=xcxc";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uri);

$data = '{"Request":{"rel":{"sta":"0"}}}';

$headers= array('Authorization:Basic xxxxxx=','Content-Type: application/json'); 

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);

curl_setopt($ch, CURLOPT_POSTFIELDS,$data); 

curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
# Send request.
$result = curl_exec($ch);
curl_close($ch);

请求没有通过并返回 HTTP/1.0 401 Unauthorized WWW-Authenticate。授权密钥是正确的,但是传递密钥的方式是否正确(通过数组)?

我在这里做错了什么?

【问题讨论】:

  • 结果变量返回什么??
  • 你肯定会得到一个结果。看看吧。

标签: php json curl


【解决方案1】:

让这个工作。

'Authorization:Basic xxxxxx=' 

导致问题并返回 HTTP/1.0 401 Unauthorized WWW-Authenticate(即使这是我使用 REST 客户端检查并工作的正确授权密钥)

当我改用用户名/密码组合时,它起作用了。

    curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-23
    • 2012-08-31
    • 2014-07-28
    • 2014-11-06
    • 1970-01-01
    • 2011-10-24
    • 1970-01-01
    • 2016-01-03
    相关资源
    最近更新 更多