Get contents from a file php

We can approach in two ways to get content from a file which is located in other server,

1) Using file function file_get_contents()

file_get_contents(< file URL >);

2) Using curl

For doing this by using curl, you need to add php curl library. If you are working on LAMP server, to install php curl, do as follows:

Open the terminal.

Run the command sudo apt-get install php5-curl

After execution restart your web server.

If you are on WAMP server, to install php curl library,

Open the php.ini file

Remove # in front of the php curl which you can found in extension/modules including section.

Restart your web server.

Before removing #, please ensure that the php_curl.dll file exists in installation path of WAMP server [Example – C:wampbinphpphp5.3.4ext]

Once you finished the installation of php curl library, use the code given below to get the file contents which is located in another server:

$link = '<URL>';
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch,CURLOPT_URL,$link);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result=curl_exec($ch);
curl_close($ch); 

One thought on “Get contents from a file php

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">