![](/img/trans.png)
[英]How to send a SOAP request with an authentication header in PHP
[英]Send Authentication header with PHP
我对API还是很陌生,但是我需要发送一个Authentication标头以及对XML数据文件的请求。
我有以下脚本,用于在返回XML文件时对其进行处理
<?php
$xml=simplexml_load_file("https://api.familysearch.org/reservation/v1/person/L81G-M81") or die("Error: Cannot create object");
print_r($xml->persons->person->baptism);
?>
唯一的问题是,它向我的请求返回状态代码401未经身份验证。
有人可以告诉我如何随我的请求发送身份验证标头吗? 我正在使用访问令牌进行身份验证。 同样,我是使用API的新手,希望能提供帮助。
您可以使用curl来发送标头,如下所示:
$service_url = 'https://api.familysearch.org/reservation/v1/person/L81G-M81';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "username:password"); //Your credentials here
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
$curl_res = curl_exec($curl);
$response = json_decode($curl_res);
curl_close($curl);
var_dump($response);
验证是否已为ssl连接启用openssl模块。
编辑:
发送带有令牌的标头是一个OAuth示例。
$headr = array();
$headr[] = 'Content-length: 0';
$headr[] = 'Content-type: application/json';
$headr[] = 'Authorization: OAuth '.$accesstoken;
curl_setopt($crl, CURLOPT_HTTPHEADER,$headr);
要测试命令,可以使用命令行:
curl -H "Authorization: Bearer <ACCESS_TOKEN>" http://www.example.com
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.