使用cURL和PHP从iTunes检索RSS提要


Retrieving RSS Feed from iTunes using cURL and PHP

我一直在尝试让一些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版本、service pack和内部版本号。(不知道为什么需要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/a终端运行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);

干杯。。。。。