Good day. There was such a problem: when parsing, file_get_contents or curl_setopt do not accept the link from the array, there are many links in the file if you write like this (line 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); 

then everything works, and if so, for example, or through a cycle:

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

then no, the links are absolutely the same, I tried it like this:

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

the same, manually typed link works, from the array - no. What could be the problem?

  • Do this with curl_setopt($curl_handle, CURLOPT_URL, trim($data[1])); - Visman
  • Thanks, it turned out. - Sergey

1 answer 1

Try:

 file('data.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); 

Note: Each line in the resulting array will be terminated with end-of-line characters (unless you specify the FILE_IGNORE_NEW_LINES flag), so if you need to get rid of these characters, you can use the rtrim () function. http://php.net/manual/ru/function.file.php

  • Thanks, it turned out that way and as above - Sergey