下午好。有这样一个问题:解析file_get_contents或curl_setopt时不接受来自数组的链接,文件中有很多链接,如果你这样写(第3行):
$data = file('data.txt');
$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_URL, 'http://сайт/каталог/еще_что-нибудь.ру');
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec($curl_handle);
curl_close($curl_handle);
然后一切正常,但如果像这样,例如,或通过循环:
$data = file('data.txt');
$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_URL, $data[1]);
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec($curl_handle);
curl_close($curl_handle);
然后没有,链接完全一样,我也试过这样:
$context = stream_context_create([
'http' => [
'method' => 'GET',
'protocol_version' => '1.1',
'header' => [
'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:24.0) Gecko/20100101 Firefox/24.0',
'Connection: close',
],
]
]);
$stream = fopen('http://url', 'r', false, $context);
$content = stream_get_contents($stream);
$data = stream_get_meta_data($stream);
同样,手动键入的链接可以从数组中工作-不。可能是什么问题呢?
尝试:
注意:结果数组中的每一行都将以行尾字符终止(除非您指定了 FILE_IGNORE_NEW_LINES 标志),因此如果您需要删除这些字符,可以使用 rtrim() 函数。 http://php.net/manual/en/function.file.php