我正在用PHP构建一个REST web服务客户端,目前我正在使用curl向该服务发出请求.
如何使用curl发出经过身份验证(http basic)的请求?我必须自己添加标题吗?
我正在用PHP构建一个REST web服务客户端,目前我正在使用curl向该服务发出请求.
如何使用curl发出经过身份验证(http basic)的请求?我必须自己添加标题吗?
你想要这个:
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
Zend有一个REST客户端和Zend_http_客户端,我相信PEAR有某种包装器.
因此,整个请求可能如下所示:
$ch = curl_init($host);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml', $additionalHeaders));
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payloadName);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($ch);
curl_close($ch);