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

我如何使用curl进行身份验证(http基本)请求?我必须自己添加标题吗?


当前回答

与SOAP不同,REST不是一个标准化的协议,因此拥有一个“REST客户端”有点困难。但是,由于大多数基于rest的服务使用HTTP作为其底层协议,因此您应该能够使用任何HTTP库。除了cURL, PHP还通过PEAR提供了以下功能:

HTTP_Request2

这所取代

HTTP_Request

一个他们如何做HTTP基本认证的例子

// This will set credentials for basic auth
$request = new HTTP_Request2('http://user:password@www.example.com/secret/');

也支持文摘认证

// This will set credentials for Digest auth
$request->setAuth('user', 'password', HTTP_Request2::AUTH_DIGEST);

其他回答

你只需要指定CURLOPT_HTTPAUTH和CURLOPT_USERPWD选项:

$curlHandler = curl_init();

$userName = 'postman';
$password = 'password';

curl_setopt_array($curlHandler, [
    CURLOPT_URL => 'https://postman-echo.com/basic-auth',
    CURLOPT_RETURNTRANSFER => true,

    CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
    CURLOPT_USERPWD => $userName . ':' . $password,
]);

$response = curl_exec($curlHandler);
curl_close($curlHandler);

或者指定头文件:

$curlSecondHandler = curl_init();

curl_setopt_array($curlSecondHandler, [
    CURLOPT_URL => 'https://postman-echo.com/basic-auth',
    CURLOPT_RETURNTRANSFER => true,

    CURLOPT_HTTPHEADER => [
        'Authorization: Basic ' . base64_encode($userName . ':' . $password)
    ],
]);

$response = curl_exec($curlSecondHandler);
curl_close($curlSecondHandler);

狂饮的例子:

use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;

$userName = 'postman';
$password = 'password';

$httpClient = new Client();

$response = $httpClient->get(
    'https://postman-echo.com/basic-auth',
    [
        RequestOptions::AUTH => [$userName, $password]
    ]
);

print_r($response->getBody()->getContents());

看到https://github.com/andriichuk/php-curl-cookbook基本认证

CURLOPT_USERPWD基本上发送user:password字符串的base64,带有http头,如下所示:

Authorization: Basic dXNlcjpwYXNzd29yZA==

所以除了CURLOPT_USERPWD之外,你还可以使用HTTP-Request头选项,就像下面的其他头一样:

$headers = array(
    'Content-Type:application/json',
    'Authorization: Basic '. base64_encode("user:password") // <---
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

你想要的是:

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

Zend有一个REST客户端和zend_http_client,我确信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);

Yahoo有一个使用PHP调用REST服务的教程:

让雅虎!使用PHP调用Web服务REST

我自己没有使用过,但雅虎就是雅虎,至少应该保证一定程度的质量。但是,它们似乎不包括PUT和DELETE请求。

另外,用户对curl_exec()和其他的贡献注释包含了很多好的信息。

与SOAP不同,REST不是一个标准化的协议,因此拥有一个“REST客户端”有点困难。但是,由于大多数基于rest的服务使用HTTP作为其底层协议,因此您应该能够使用任何HTTP库。除了cURL, PHP还通过PEAR提供了以下功能:

HTTP_Request2

这所取代

HTTP_Request

一个他们如何做HTTP基本认证的例子

// This will set credentials for basic auth
$request = new HTTP_Request2('http://user:password@www.example.com/secret/');

也支持文摘认证

// This will set credentials for Digest auth
$request->setAuth('user', 'password', HTTP_Request2::AUTH_DIGEST);