如何在 PHP curl 中使用基本授权 [英] How to use basic authorization in PHP curl

查看:46
本文介绍了如何在 PHP curl 中使用基本授权的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用基本授权的 PHP curl 请求时遇到问题.

I am having problem with PHP curl request with basic authorization.

这是命令行卷曲:

curl -H "Accept: application/product+xml" "https://{id}:{api_key}@api.domain.com/products?limit=1&offset=0"

我尝试通过以下方式设置 curl 标头,但它不起作用

I have tried by setting curl header in following ways but it's not working

Authorization: Basic id:api_key
or 
Authorization: Basic {id}:{api_key}

我收到响应请求中的身份验证参数丢失或无效",但我使用了正确的 id 和 api_key,它们在命令行 curl 中工作(我测试过)

I get the response "authentication parameter in the request are missing or invalid" but I have used proper id and api_key which is working in command line curl (I tested)

请帮帮我.

推荐答案

试试下面的代码:

$username='ABC';
$password='XYZ';
$URL='<URL>';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$URL);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$result=curl_exec ($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);   //get status code
curl_close ($ch);

这篇关于如何在 PHP curl 中使用基本授权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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