【问题标题】:Bypass authenticated XML page to be parsed with PHP?绕过经过身份验证的 XML 页面以使用 PHP 解析?
【发布时间】:2010-09-21 22:44:04
【问题描述】:

如何解析这个 xml 页面 http://evercore:david10@feeds.outgard.net/www.sportsbook.com/trends/203.xml 使用 PHP simplexml?我收到一条错误消息,提示无法加载。

谢谢, S

【问题讨论】:

  • 用户名/密码好像错了。
  • 哦。它的evercore 不是evernote

标签: php xml authentication curl


【解决方案1】:

您需要欺骗代理以使其接受请求。

使用此 PHP 代码获取结果:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://feeds.outgard.net/www.sportsbook.com/trends/203.xml');
curl_setopt($ch, CURLOPT_GET, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, 'evercore:david10');
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT     5.0)"); 

$data = curl_exec($ch);
echo $data;

// Process data with simple XML

curl_close($ch); 

【讨论】:

  • 现在我明白了。谢谢!这工作正常,但我在顶部收到此错误警告:curl_setopt() 预计参数 2 很长,第 4 行的 /Users/ginocortez/phpprojects/testing/xmlcurl.php 中给出的字符串
【解决方案2】:

一直在玩这个,直到@vassilis 指出应用了用户代理嗅探,将其视为实现同一目标的另一种方式,指向@vassilis。

<?php
$username = 'evercore';
$password = 'david10';
$opts = array(
    'http' => array(
        'method'  => 'GET',
        'header'  => sprintf("Authorization: Basic %s\r\nUser-Agent: Foobar!", base64_encode($username.':'.$password))
    )
);

$context = stream_context_create($opts);
libxml_set_streams_context($context);
$xml =  new SimpleXMLElement(
        "http://feeds.outgard.net/www.sportsbook.com/trends/203.xml",
        null,
        true);
var_dump($xml);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-25
    相关资源
    最近更新 更多