【问题标题】:Retrieving RSS Feed from iTunes using cURL and PHP使用 cURL 和 PHP 从 iTunes 检索 RSS 源
【发布时间】:2015-03-03 16:22:44
【问题描述】:

一直在尝试让一些 PHP cURL 代码工作,当你给它播客 URL 时,它会从 iTunes 获取 RSS 提要。代码如下:

$inputString = "curl -A 'iTunes/12.1.1.4 (Windows; U; Microsoft Windows 7 Home Premium Edition Service Pack 1 (Build 7601) DPI/96' -s 'https://itunes.apple.com/podcast/id530114975'";  
$input = shell_exec($inputString);
$dom = new DOMDocument();
$html = $dom->loadHTML($input);

使用 shell_exec 执行的 cURL 调用返回一个空白字符串。 当我调用 loadHTML 函数时,它会给出以下错误,这很明显,因为 cURL 调用没有返回任何内容.....

Warning: DOMDocument::loadHTML(): Empty string supplied as input in C:\php scripts\itunesFeedExtractor.php on line 130

现在,我从其他地方获得了 PHP cURL 代码,以前没有使用过它,并尝试对其进行修改以匹配我的计算机设置....我已经更改了 Windows 版本、服务包、版本号。 (不知道为什么需要 DPI/96 所以我不管它)

【问题讨论】:

  • 有一个 PHP curl 扩展,你最好使用它。无需 shell exec curl。
  • 尝试使用下面的 php curl 扩展,但仍然返回一个空字符串。想知道 curl 调用中的设置是否不正确?

标签: php ios curl rss itunes


【解决方案1】:

最好使用 PHP curl 扩展:

$ch = curl_init("https://itunes.apple.com/podcast/id530114975");
curl_setopt($ch, CURLOPT_USERAGENT, "iTunes/12.1.1.4 (Windows; U; Microsoft Windows 7 Home Premium Edition Service Pack 1 (Build 7601) DPI/96");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);

但如果您真的想使用 shell_exec 方法,请确保 curl 在您的路径中 - 您可以通过从 cmd / 终端运行 curl 命令来检查

【讨论】:

  • 好吧,我已经安装了 PHP cURL 的东西,并且正在找到 curl_init() 函数。但它仍然像以前一样返回一个空字符串。 (顺便说一句,curl 在命令行中工作)
  • curl_setopt($ch, CURLOPT_USERAGENT, "iTunes/12.1.1.4 (Windows; U; Microsoft Windows 7 Home Premium Edition Service Pack 1 (Build 7601) DPI/96");$ch = curl_init( "itunes.apple.com/podcast/id530114975");curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch);看起来我通过添加两个来让它工作额外的设置.... 新的 PHP cURL 版本看起来像这样:
【解决方案2】:

好吧,我通过添加更多 curl_setopt() 选项让它工作了。现在完整的代码如下:

$ch = curl_init("https://itunes.apple.com/podcast/id530114975");
curl_setopt($ch, CURLOPT_USERAGENT, "iTunes/12.1.1.4 (Windows; U; Microsoft Windows 7 Home Premium Edition Service Pack 1 (Build 7601) DPI/96");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);

干杯.....

【讨论】:

    猜你喜欢
    • 2010-12-31
    • 1970-01-01
    • 1970-01-01
    • 2012-12-25
    • 1970-01-01
    • 2015-10-05
    • 2012-04-15
    • 2015-12-10
    • 1970-01-01
    相关资源
    最近更新 更多