如何使用PHP curl使用HTTP基本身份validation进行请求?

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

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

你要这个:

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

Zend有一个REST客户端和zend_http_client,我确信PEAR有一些包装。 但它很容易做到你自己。

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

 $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); 

CURLOPT_USERPWD基本上发送user:password的base64 user:passwordstring与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是最简单和最原始的方式。

这适用于我:

 <?php $login = 'login'; $password = 'password'; $url = 'http://your.url'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($ch, CURLOPT_USERPWD, "$login:$password"); $result = curl_exec($ch); curl_close($ch); echo($result); 

与SOAP不同的是,REST不是一个标准化的协议,所以有一个“REST客户端”有点困难。 但是,由于大多数RESTful服务使用HTTP作为其底层协议,因此您应该可以使用任何HTTP库。 除了cURL之外,PHP还通过PEAR来实现这些function:

HTTP_Request2

取而代之

HTTP_REQUEST

他们如何做HTTP基本身份validation的示例

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

也支持Digest Auth

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

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

让雅虎! Web服务REST调用PHP

我自己没有用过,但雅虎是雅虎,至less应该保证一定的质量水平。 但是他们似乎没有涵盖PUT和DELETE请求。

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

迈克尔·道林(Michael Dowling)非常积极地保持着Guzzle是一个很好的select。 除了优雅的界面,asynchronous调用和PSR合规性之外,它使REST调用的authentication头文件变得简单:

 // Create a client with a base URL $client = new GuzzleHttp\Client(['base_url' => 'http://myservices.io']); // Send a request to http://myservices.io/status with basic authentication $response = $client->get('/status', ['auth' => ['username', 'password']]); 

看文档 。

如果授权types是基本身份validation,并且发布的数据是json,那么可以这样做

 <?php $data = array("username" => "test"); // data u want to post $data_string = json_encode($data); $api_key = "your_api_key"; $password = "xxxxxx"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "https://xxxxxxxxxxxxxxxxxxxxxxx"); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_USERPWD, $api_key.':'.$password); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Accept: application/json', 'Content-Type: application/json') ); if(curl_exec($ch) === false) { echo 'Curl error: ' . curl_error($ch); } $errors = curl_error($ch); $result = curl_exec($ch); $returnCode = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); echo $returnCode; var_dump($errors); print_r(json_decode($result, true)); 

那里有多个REST框架。 我强烈build议查看Slim mini Framework for PHP
这是其他人的列表。