PHP: read a remote file (ideally using fopen)

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP

PHP: read a remote file (ideally using fopen)



I'd like to read a remote text file (ideally using fopen) using PHP. My script works using fopen when I'm using this function on a local file.



I've tried:


$file = fopen ("http://abc.abc.abc", "r");
if (!$file)
echo "<p>Unable to open remote file.n";
exit;



and I got:



Warning: fopen(http://abc.abc.abc): failed to open stream: No connection could be made because the target machine actively refused it. in C:xampphtdocsNMRnmrTest5.php on line 2 Unable to open remote file.



I've read that phpseclib could be a good option and since I can access to my files using WinSCP (SFTP) or by using Puttyfor I tried this (after copying all the files from phpseclib to my directory) hoping that I could copy locally the file and then read it with fopen (not the best thing for met but I could live with that):


include('Net/SFTP.php');

$sftp = new Net_SFTP('abc.abc.abc');
if (!$sftp->login('username', 'pass'))
exit('Login Failed');



and I got:



Notice: No compatible server to client encryption algorithms found in C:xampphtdocsNMRNetSSH2.php on line 1561
Login Failed



Interstingly, I got a different message if I was connected to the server (using WinSCP):



Notice: Error reading from socket in C:xampphtdocsNMRNetSSH2.php on line 3362



Notice: Connection closed by server in C:xampphtdocsNMRNetSSH2.php on line 1471
Login Failed



Any idea on how I could get it to work? Ideally I would use fopen but I'm open to other solution.




1 Answer
1



I have faced similiar issues with fopen.
Curl is useful for these purposes.



Please check with the following basic example function(if the url is https, please uncomment the CURLOPT_SSL_VERIFYPEER = FALSE line).


$url = '***THE URL***';
$result = get_web_page_by_curl($url);

if ($result['errno'] != 0) echo 'error: bad url, timeout, redirect loop ...';
if ($result['http_code'] != 200) echo 'error: no page, no permissions, no service ...';
else
$page = $result['content'];
echo $page;


function get_web_page_by_curl($url)
$agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)";

$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => $agent, // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
//CURLOPT_SSL_VERIFYPEER => FALSE // this line makes it work under https
);

$ch = curl_init($url);
curl_setopt_array($ch, $options);
$content = curl_exec($ch);
$err = curl_errno($ch);
$errmsg = curl_error($ch);
$header = curl_getinfo($ch);
curl_close($ch);

$header['errno'] = $err;
$header['errmsg'] = $errmsg;
$header['content'] = $content;
return $header;






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

Executable numpy error

PySpark count values by condition

Mass disable jenkins jobs