php curl http basic,rest - 如何使用PHP curl使用HTTP基本身份验证发出请求?

rest - 如何使用PHP curl使用HTTP基本身份验证发出请求?

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

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

9个解决方案

317 votes

你要这个:

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

mr-sk answered 2019-03-29T10:44:15Z

90 votes

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

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

Sabuj Hassan answered 2019-03-29T10:44:47Z

28 votes

最简单和原生的方式是直接使用CURL。

这对我有用:

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

Fedir RYKHTIK answered 2019-03-29T10:45:14Z

7 votes

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

HTTP_Request2

取而代之的

HTTP_REQUEST

他们如何进行HTTP Basic Auth的示例

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

nategood answered 2019-03-29T10:46:14Z

4 votes

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

制造雅虎! 使用PHP调用Web服务REST

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

此外,curl_exec()和其他人的用户贡献注释包含许多好的信息。

Pekka 웃 answered 2019-03-29T10:47:01Z

3 votes

Michael Dowling非常积极地保持Guzzle是一个很好的方式。 除了优雅的界面,异步调用和PSR兼容性之外,它还使REST调用的身份验证标头变得简单:

// 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']]);

查看文档。

Jannie Theunissen answered 2019-03-29T10:47:32Z

0 votes

如果授权类型是Basic auth并且发布的数据是json,那么这样做

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

Sunny Vashisht answered 2019-03-29T10:47:57Z

0 votes

对于那些不想使用卷曲的人:

//url

$url = 'some_url';

//Credentials

$client_id = "";

$client_pass= "";

//HTTP options

$opts = array('http' =>

array(

'method' => 'POST',

'header' => array ('Content-type: application/json', 'Authorization: Basic '.base64_encode("$client_id:$client_pass")),

'content' => "some_content"

)

);

//Do request

$context = stream_context_create($opts);

$json = file_get_contents($url, false, $context);

$result = json_decode($json, true);

if(json_last_error() != JSON_ERROR_NONE){

return null;

}

print_r($result);

Wouter answered 2019-03-29T10:48:22Z

-5 votes

有多个REST框架。 我强烈建议您查看适用于PHP的Slim mini Framework

这是其他人的名单。

rudder answered 2019-03-29T10:48:53Z

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值