如何在PHP中添加curl HTTP头进行身份验证? [英] How to add curl HTTP Headers for authentication in PHP?
问题描述
我想添加HTTP头来验证Udemy API访问。有人告诉我如何添加headers.I已经有客户端id和密钥。我想从PHP页面访问API。
https://developers.udemy.com/
这是我尝试的代码使用:
$ ch = curl_init($ request);
curl_setopt($ ch,CURLOPT_URL,$ request);
curl_setopt($ ch,CURLOPT_HTTPHEADER,array('X-Udemy-Client-Id:MY_ID','X-Udemy-Client-Secret:Secret'));
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,TRUE);
$ results = curl_exec($ ch);
echo $ results;
输出:
code>空白页
有人可以指出问题的原因吗?
正如@drmarvelous写道,您执行两个请求(第一个是CURL,第二个是 file_get_contents
),这样做是一样的。其中CURL请求的结果实际上并未在您的脚本中使用。它使用在没有认证参数的情况下执行的 file_get_contents
请求的结果。因为你得到未授权的
错误。
所以你必须使用CURL请求的结果:
...
$ json = json_decode($ results,true);
print_r($ json);
更新:
您必须确保为API请求使用有效的网址,即代码中的
$ request
的值应为有效网址。此外,请确保您通过HTTP标头传递有效的身份验证参数(Client-Id
和Client-Secret
)。
此外,由于API是安全的,您必须通过将
CURLOPT_SSL_VERIFYPEER
选项设置为false
。
因此代码应如下所示:
$ ch = curl_init($ request);
curl_setopt($ ch,CURLOPT_URL,$ request);
curl_setopt($ ch,CURLOPT_HTTPHEADER,array('X-Udemy-Client-Id:{YourID}','X-Udemy-Client-Secret:{YourSecret}'));
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ ch,CURLOPT_SSL_VERIFYPEER,false);
$ results = curl_exec($ ch);
echo $ results;
I want to add the HTTP headers for authenticating Udemy API access.Can someone tell me as to how to add the headers.I already have the client id and secret key.I want to access the API from a PHP page. https://developers.udemy.com/ Here is the code i tried using:
$ch = curl_init($request); curl_setopt($ch, CURLOPT_URL, $request); curl_setopt($ch,CURLOPT_HTTPHEADER,array('X-Udemy-Client-Id:MY_ID','X-Udemy-Client-Secret:Secret')); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); $results= curl_exec($ch); echo $results;
Output:
Blank Page
Can someone point out what the problem might be?
解决方案As @drmarvelous wrote, you perform two requests (1st - by CURL, and 2nd - by
file_get_contents
) which does the same. Wherein the result of CURL request is not actually used in your script. It use the result offile_get_contents
request which is performed without authentication parameters. Because of this you gettingUnauthorized
error.So you have to use the result of CURL request:
... $json = json_decode($results, true); print_r($json);
Update:
You have to ensure you use valid URL for API request, i.e. value of
$request
in your code should be valid URL. Also, ensure you pass valid authentication parameters (Client-Id
andClient-Secret
) by HTTP headers.Furthermore, since API is secured, you have to disable SSL peer verification by setting
CURLOPT_SSL_VERIFYPEER
option tofalse
.So the code should look like this:
$ch = curl_init($request); curl_setopt($ch, CURLOPT_URL, $request); curl_setopt($ch,CURLOPT_HTTPHEADER,array('X-Udemy-Client-Id: {YourID}','X-Udemy-Client-Secret: {YourSecret}')); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $results= curl_exec($ch); echo $results;
这篇关于如何在PHP中添加curl HTTP头进行身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!