使用 cURL 和 PHP 从 iTunes 检索 RSS Feed

Retrieving RSS Feed from iTunes using cURL and PHP

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

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

cURL 调用时使用shell_exec returns 一个空字符串执行。 当我调用 loadHTML 函数时,它给出了以下错误,这是非常明显的,因为 cURL 调用没有 return 任何东西......

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

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

你最好使用 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 命令检查

好吧,我通过添加更多 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);

干杯......