我正在尝试从 CLI 运行 PHP 脚本,而 curl 似乎返回一个空字符串。
如果我从浏览器运行脚本,它可以工作,但当我从 CLI 运行它时就不行。只是试图让它在 wamp 上本地工作。
我知道这里php.ini
使用了两个不同的文件。也许我在那里遗漏了什么?我可以确认 curl 扩展程序已加载extension_loaded('curl')
.
代码:
function download_page($path) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $path);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 0);
$retValue = curl_exec($ch);
curl_close($ch);
return $retValue;
}
(上面的代码在这里找到)
并加载它:
$xml = new DomDocument();
$page = download_page("http://localhost/MySite/MyFile.xml");//In reality this file will be hosted on some webserver
$xml->loadXML($page); // Error here - empty string
// .. process XML file