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

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

问题描述

我有基本授权的PHP curl请求的问题。

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

这是命令行curl:

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天全站免登陆