我正在尝试实现一个PHP方法来验证对美联社提要的请求。从他们的医生那里:
要对所有提要和内容请求进行身份验证,AP WebFeeds系统使用,该身份验证目前是聚合提要的标准。大多数提要阅读器和读取器组件允许提前配置用户凭据,并将它们传递到头中而不是URL中。 重要:直接在中传递凭据目前被广泛阻止。有关更多信息,请参见微软在http://www.microsoft.com/technet/security/bulletin/MS04-004.mspx的安全公告。 可以将读取器或组件配置为以下列格式创建具有名称/值的身份验证标头:
("Authorization", "Basic " + {Encoded_username_and_password})将{Encoded_username_and_password}替换为字符串“用户名:密码”中字节的Base64编码。 如果您正在编写自己的客户端代码来下载提要,请使用内置到编程语言库中的HTTP基本身份验证。大多数平台上都有HTTP基本身份验证,例如Perl、PHP、C或Java。
我的尝试是:
/**
* Begin Transaction
*/
$url = "http://syndication.ap.org/AP.Distro.Feed/GetFeed.aspx?idList=" . implode(',', $idList) . "&idListType=products&maxItems=25";
$auth = "Authorization=Basic " . base64_encode($user . ":" . $pass);
$ch = curl_init(); // initialize curl handle
curl_setopt($ch, CURLOPT_URL, $url); // set url to post to
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
curl_setopt($ch, CURLOPT_POST, 1); // set POST method
curl_setopt($ch, CURLOPT_POSTFIELDS, $auth); // add POST fields
$result = curl_exec($ch); // run the whole process
curl_close($ch);
/**
* End Transaction
*/
var_dump($result);这给了我:
string(405) "
Authentication
FeedServer
Authentication Failed on GetFeed
Authentication denied no auth credentials detected
"在PHP中验证此请求的正确方法是什么?
(还请参阅提供的ASP.NET和Java示例。)
发布于 2012-07-18 01:27:43
授权是HTTP头不是POST参数。您使用了CURLOPT_POSTFIELDS,它设置请求的主体。相反,您需要使用CURLOPT_HTTPHEADER来设置它(它的条目在列表中很低):
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Basic ' . base64_encode($user . ":" . $pass)));如果这还不明显,则不需要将其作为post请求(除非API需要),因此可以删除:
curl_setopt($ch, CURLOPT_POST, 1); // set POST method发布于 2013-05-21 12:21:33
您还可以使用以下代码:
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $user . ':' . $pass);我刚试过,效果很好。
希望能帮上忙。致以问候。
https://stackoverflow.com/questions/11532938
复制相似问题