Questions tagged [file-get-contents]

In PHP, file_get_contents is the preferred way to read the contents of a file into a string. It will use memory mapping techniques (if supported by your OS) to enhance performance.

file-get-contents
Filter by
Sorted by
Tagged with
371 votes
17 answers
426k views

How can I handle the warning of file_get_contents() function in PHP? [duplicate]

I wrote a PHP code like this $site="http://www.google.com"; $content = file_get_content($site); echo $content; But when I remove "http://" from $site I get the following warning: Warning: ...
Waseem's user avatar
  • 11.9k
338 votes
3 answers
410k views

How to post data in PHP using file_get_contents?

I'm using PHP's function file_get_contents() to fetch contents of a URL and then I process headers through the variable $http_response_header. Now the problem is that some of the URLs need some data ...
Paras Chopra's user avatar
  • 4,109
282 votes
21 answers
514k views

file_get_contents(): SSL operation failed with code 1, Failed to enable crypto

I’ve been trying to access this particular REST service from a PHP page I’ve created on our server. I narrowed the problem down to these two lines. So my PHP page looks like this: <?php $...
Joe's user avatar
  • 8,491
192 votes
6 answers
184k views

Does file_get_contents() have a timeout setting?

I am calling a series of links using the file_get_contents() method in a loop. Each link may take more than 15 minutes to process. Now, I worry about whether PHP's file_get_contents() has a timeout ...
Flora Clinton's user avatar
123 votes
4 answers
125k views

PHP cURL vs file_get_contents

How do these two pieces of code differ when accessing a REST API? $result = file_get_contents('http://api.bitly.com/v3/shorten?login=user&apiKey=key&longUrl=url'); and $ch = curl_init('http:...
Salvador Dali's user avatar
118 votes
11 answers
350k views

How to get file_get_contents() to work with HTTPS?

I'm working on setting up credit card processing and needed to use a workaround for CURL. The following code worked fine when I was using the test server (which wasn't calling an SSL URL), but now ...
James Simpson's user avatar
113 votes
17 answers
496k views

PHP file_get_contents() returns "failed to open stream: HTTP request failed!"

I am having problems calling a URL from PHP code. I need to call a service using a query string from my PHP code. If I type the URL into a browser, it works ok, but if I use file-get-contents() to ...
undefined's user avatar
  • 5,250
84 votes
8 answers
105k views

file_get_contents when url doesn't exist

I'm using file_get_contents() to access a URL. file_get_contents('http://somenotrealurl.com/notrealpage'); If the URL is not real, it return this error message. How can I get it to error gracefully ...
sami's user avatar
  • 7,585
79 votes
11 answers
139k views

file_get_contents() Breaks Up UTF-8 Characters

I am loading a HTML from an external server. The HTML markup has UTF-8 encoding and contains characters such as ľ,š,č,ť,ž etc. When I load the HTML with file_get_contents() like this: $html = ...
Richard Knop's user avatar
  • 82.3k
53 votes
7 answers
216k views

Show image using file_get_contents

how can I display an image retrieved using file_get_contents in php? Do i need to modify the headers and just echo it or something? Thanks!
Belgin Fish's user avatar
  • 19.6k
52 votes
12 answers
68k views

Replace a whole line where a particular word is found in a text file

How can I replace a particular line of text in file using php? I don't know the line number. I want to replace a line containing a particular word.
kishore's user avatar
  • 1,017
50 votes
6 answers
115k views

Alternative to file_get_contents?

$xml_file = file_get_contents(SITE_PATH . 'cms/data.php'); The problem is that a server has URL file-access disabled. I cannot enable it, its a hosting thing. So the question is this. The data.php ...
ComputerUser's user avatar
  • 4,838
50 votes
2 answers
45k views

Upload a file using file_get_contents

I realise I can do this with CURL very easily, but I was wondering if it was possible to use file_get_contents() with the http stream context to upload a file to a remote web server, and if so, how?
Shabbyrobe's user avatar
  • 12.5k
49 votes
5 answers
308k views

Get file content from URL?

When I use following URL in browser then it prompt me to download a text file with JSOn content. https://chart.googleapis.com/chart?cht=p3&chs=250x100&chd=t:60,40&chl=Hello|World&chof=...
Awan's user avatar
  • 18.4k
48 votes
8 answers
199k views

PHP ini file_get_contents external url

I use following PHP function: file_get_contents('http://example.com'); Whenever I do this on a certain server, the result is empty. When I do it anywhere else, the result is whatever the page's ...
arik's user avatar
  • 28.8k
43 votes
4 answers
42k views

PHP Parallel curl requests

I am doing a simple app that reads json data from 15 different URLs. I have a special need that I need to do this serverly. I am using file_get_contents($url). Since I am using file_get_contents($url)...
user1205408's user avatar
43 votes
4 answers
97k views

How to use CURL instead of file_get_contents?

I use file_get_contents function to get and show external links on my specific page. In my local file everything is okay, but my server doesn't support the file_get_contents function, so I tried to ...
Morteza's user avatar
  • 2,153
38 votes
1 answer
56k views

Laravel5 Json get file contents

I'm using Laravel-5 right now and I've just created a JSON file called test(test.json). My question is: Where should I put my JSON file? How can I use file_get_contents and point to that one file I ...
KMackinley's user avatar
35 votes
2 answers
66k views

Why I'm getting 500 error when using file_get_contents(), but works in a browser?

$html = file_get_contents("https://www.[URL].com"); echo $html; produces this in the error logs: PHP Warning: file_get_contents(https://www.[URL].com) [function.file-get-contents]: ...
bdev's user avatar
  • 2,060
33 votes
6 answers
81k views

Get content between two strings PHP

Whats is the best way to obtain the content between two strings e.g. ob_start(); include('externalfile.html'); ## see below $out = ob_get_contents(); ob_end_clean(); preg_match('/{FINDME}(.|\n*)+{\/...
Lizard's user avatar
  • 44.3k
31 votes
4 answers
52k views

file_get_contents throws 400 Bad Request error PHP

I'm just using a file_get_contents() to get the latest tweets from a user like this: $tweet = json_decode(file_get_contents('http://api.twitter.com/1/statuses/user_timeline/User.json')); This works ...
Javier Villanueva's user avatar
31 votes
3 answers
53k views

PHP - Posting JSON via file_get_contents

I am trying to POST JSON content to a remote REST endpoint, however the 'content' value appears to be empty on delivery. All other headers etc are being received correctly, and the web service tests ...
Ben's user avatar
  • 6,076
30 votes
8 answers
91k views

How to resolve the error "[ErrorException] file_get_contents(/var/www/laravel/.env): failed to open stream: No such file or directory"? [duplicate]

I'm using Ubuntu 14.04 on my machine. I installed composer and then laravel in the document root i.e. /var/www I also gave -R 777 persmission to folder laravel present in directory /var/www Then I ...
PHPLover's user avatar
28 votes
1 answer
13k views

Need response body of HTTP 500 with file_get_contents (PHP)

Using file_get_contents as part of custom SOAP implementation to apply SOAP calls (ALL libraries that we tried would not do SSL + certificate based authentication with SOAP 1.2 correctly). However ...
frEEk's user avatar
  • 283
27 votes
1 answer
43k views

PHP basic auth file_get_contents() [duplicate]

I need to parse some XML data from a website. The XML data is in raw format, but before I need to authentificate (basic webserver based auth, with username & password). I tried: $homepage = ...
dehniz's user avatar
  • 275
26 votes
3 answers
44k views

PHP - Content-type not specified assuming application/x-www-form-urlencoded

For 2 days I'm having trouble with my PHP script on my server. I've changed nothing and suddenly it didn't work anymore. Here is the code: $query = http_build_query($data); $options = array( 'http'...
hannsch's user avatar
  • 295
24 votes
9 answers
69k views

How to execute and get content of a .php file in a variable?

I want to get contents of a .php file in a variable on other page. I have two files, myfile1.php and myfile2.php. myfile2.php <?PHP $myvar="prashant"; // echo $myvar; ?> Now I want ...
djmzfKnm's user avatar
  • 27k
24 votes
10 answers
107k views

How to get the content-type of a file in PHP?

I'm using PHP to send an email with an attachment. The attachment could be any of several different file types (pdf, txt, doc, swf, etc). First, the script gets the file using "file_get_contents". ...
edt's user avatar
  • 22.2k
24 votes
6 answers
96k views

file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name or service not known

I'm trying to download an image from a server using a PHP script on my website on xampp server. The image is being downloaded using the function file_get_contents. The php code for downloading on ...
user3007493's user avatar
23 votes
4 answers
24k views

How to get MIME-type of an image with file_get_contents in PHP

I need to get the MIME type of an image, but I only have the body of the image which I've got with file_get_contents. Is there a possibility to get the MIME type?
Zwen2012's user avatar
  • 3,458
23 votes
6 answers
104k views

file_get_contents - failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found

I'm having some weird problems with file_get_contents after moving my site to a new domain. I had to set up a new domain and IP address (using Plesk) to get a new ssl certificate working. Now my ...
user1250758's user avatar
22 votes
3 answers
152k views

get url content PHP [duplicate]

I wanna put the content of a URL in a string and the process it. However, I have a problem. I get this error: Warning: file_get_contents(http://www.findchips.com/avail?part=74ls244) [function.file-...
Amir Tugi's user avatar
  • 2,406
22 votes
3 answers
158k views

JSON to PHP Array using file_get_contents

I am trying to fetch the below json content using a magazine api. The output of the json is like this. i want the below json to convert to php array. { "bpath": "http://www.sampledomain.com/", "clist"...
hjaffer2001's user avatar
21 votes
3 answers
24k views

file_get_contents() equivalent for Node.JS

I was wondering if there was any file_get_contents() equivalents in Node.JS modules or elsewhere. It has to lock the process until the download is finished, so the existing request() code in Node.js ...
Kyle Hotchkiss's user avatar
20 votes
5 answers
40k views

PHP sending variables to file_get_contents()

I want to be able to send a few variables to a file through file_get_contents(). This is firstfile.php: <?php $myvar = 'This is a variable'; // need to send $myvar into secondfile.php $mystr = ...
Richard Westington's user avatar
19 votes
4 answers
29k views

How to send cookies with file_get_contents

I'm trying to get the contents from another file with file_get_contents (don't ask why). I have two files: test1.php and test2.php. test1.php returns a string, bases on the user that is logged in. ...
Ikke's user avatar
  • 100k
19 votes
8 answers
59k views

Unable to find the wrapper "https" with file_get_contents

Calling file_get_contents() with https:// urls give me the following error: warning: file_get_contents(): Unable to find the wrapper "https" - did you forget to enable it when you configured PHP? I'...
John Smith's user avatar
19 votes
2 answers
15k views

using file get contents or curl

I was ask to use a simple facebook api to return the number of likes or shares at work which return json string. Now since i am going to do this for a very large amount of links, which one is better: ...
Ibu's user avatar
  • 43.3k
18 votes
1 answer
69k views

GET Request from PHP using file_get_contents with parameters

I want to send a GET request to an external site, but also want to send some parameters for example i've to send a get request to example.com i want to execute www.example.com/send.php?uid=1&pwd=...
Hormigas's user avatar
  • 1,449
18 votes
3 answers
3k views

Retrieving website HTML page using cURL with current session and cookie data on secured page

Question fully revised on: Feb. 19th The thing i want (in short): I would like to get an HTML page using cURL that is secured with a user login(at time of cURL request the user is logged in with ...
Firewizz's user avatar
  • 793
17 votes
2 answers
13k views

Google Drive GET_CONTENT intent read file

I have an app on android that does some file sharing using cloud storages like dropbox. To start sharing I throw android.intent.action.SEND. On the list that is shown I see the Google Drive app (...
khusrav's user avatar
  • 5,277
16 votes
2 answers
76k views

Parse html table using file_get_contents to php array

I am trying to parse the table shown here into a multi-dimensional php array. I am using the following code but for some reason its returning an empty array. After searching around on the web, I found ...
Farhan Ahmad's user avatar
  • 5,158
16 votes
7 answers
25k views

How to get the mime type of a file after using file_get_contents from a remote server

I'm reading a file in PHP from Alfresco and then outputting it to the browser. The only problem is the mime type or the extension of the file. This is the code I'm using: <?php ob_start(); ...
Nicola Peluchetti's user avatar
16 votes
1 answer
14k views

PHP file_get_contents ignoring timeout?

$url = 'http://a.url/i-know-is-down'; //ini_set('default_socket_timeout', 5); $ctx = stream_context_create(array( 'http' => array( 'timeout' => 5, 'ignore_errors' => ...
Cesar's user avatar
  • 4,086
16 votes
1 answer
24k views

Is there a limit on PHP file_get_contents? [closed]

I am trying to read a large file (10M) using php file_get_contents $file = 'http://www.remoteserver.com/test.txt'; $data = file_get_contents( $file ); var_dump ( $data ); It dumps back string(32720)...
Scott Foster's user avatar
15 votes
6 answers
76k views

file_get_contents not working?

This code is not working to server. But It is working to my localhost (xampp) $url = file_get_contents('http://www.site.com/'); $xhtml='|<tr style="background-color:#dddddd;"> <td ...
Venom's user avatar
  • 159
15 votes
4 answers
24k views

Ignoring errors in file_get_contents HTTP wrapper?

The following code is to query an online thesaurus for a search engine I'm building as a college project, but I'm having problems with file_get_contents "failed to open stream" errors. When I send a ...
shanahobo86's user avatar
15 votes
6 answers
39k views

HTTP request failed! HTTP/1.1 505 HTTP Version Not Supported error

I'm trying to use file_get_contents() to get the response from a server and this error was encountered. Could someone tell me what is the reason and how to fix it? The portion of the code is: $api = "...
shyam's user avatar
  • 6,721
14 votes
6 answers
122k views

Why doesn't file_get_contents work?

Why does file_get_contents not work for me? In the test file code below, it seems that everyone's examples that I've searched for all have this function listed, but it never gets executed. Is this a ...
PVJAZZ's user avatar
  • 149
14 votes
7 answers
73k views

Simple JSON request in PHP

I have the following json country_code({"latitude":"45.9390","longitude":"24.9811","zoom":6,"address":{"city":"-","country":"Romania","country_code":"RO","region":"-"}}) and i want just the ...
Master345's user avatar
  • 2,256

1
2 3 4 5
53