如何使用HTTP基本认证与PHP curl进行请求? [英] How do I make a request using HTTP basic authentication with PHP curl?

查看:212
本文介绍了如何使用HTTP基本认证与PHP curl进行请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在PHP中构建REST Web服务客户端,目前我正在使用curl向服务发出请求。

I'm building a REST web service client in PHP and at the moment I'm using curl to make requests to the service.

如何使用curl验证(http基本)请求?

How do I use curl to make authenticated (http basic) requests? Do I have to add the headers myself?

推荐答案

您希望这样:

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

Zend有一个REST客户端和zend_http_client,并且我确信PEAR有某种包装。

Zend has a REST client and zend_http_client and I'm sure PEAR has some sort of wrapper. But its easy enough to do on your own.

所以整个请求可能看起来像这样:

So the entire request might look something like this:

$process = curl_init($host);
curl_setopt($process, CURLOPT_HTTPHEADER, array('Content-Type: application/xml', $additionalHeaders));
curl_setopt($process, CURLOPT_HEADER, 1);
curl_setopt($process, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_POST, 1);
curl_setopt($process, CURLOPT_POSTFIELDS, $payloadName);
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($process);
curl_close($process);

这篇关于如何使用HTTP基本认证与PHP curl进行请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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