How to Fix PHP File_get_contents() Not Working

See how you solve PHP file_get_contents() function not working

The PHP file_get_contents() function is used to read a file content and return it as a string.

Most of the time, this function is also used to get the content of a file.

But when you need to read a remote file, the function may not work and give a warning response instead.

For example, the following code:

<?php
$res = file_get_contents("https://google.com");

Produced the following warning:

Warning: file_get_contents(https://google.com): 
Failed to open stream: no suitable wrapper could be found 
in ... on line ...

To access a remote file using the file_get_contents() function, you need to activate the allow_url_fopen configuration.

You need to open the php.ini file loaded by your PHP server and set the allow_url_fopen configuration to On.

If you can’t find where your php.ini file is located, then run the phpinfo() function by creating a new file:

<?php
phpinfo();
?>

You need to find the php.ini location as shown below:

Find php.ini locationFind php.ini location

Open the php.ini file, then activate the allow_url_fopen config:

allow_url_fopen = On

Restart your Apache or Nginx server, and you should be able to read the content of a remote file.

Using cURL instead of file_get_contents

Sometimes, you may see that you can use file_get_contents() from a local machine but doesn’t work from a server.

This is because your server may have security protocols that prevent file_get_contents() from working.

For an alternative solution, you can use the cURL library to read the content of a remote file.

Here is an example of using cURL:

// 1. initialize cURL
$ch = curl_init();

// 2. set the URL to access
$url = "https://google.com";
curl_setopt($ch, CURLOPT_URL, $url);

// 3. set cURL to return as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// 4. execute cURL and store the result
$output = curl_exec($ch);

// 5. close cURL after use
curl_close($ch);

// 6. print the output
print $output;

You can read more about cURL options in curl_setopt documentation.

Unlike the file_get_contents() function, cURL is designed to allow data transfer from various network protocols.

It’s usually enabled by default, so you don’t need to tweak your PHP configuration.

Whenever possible, use cURL to access remote files and file_get_contents() function for local files.

Now you’ve learned how to solve file_get_contents() function not working. You’ve also learned how to replace the function with cURL.

Original article source at: https://sebhastian.com/ 

#php #file #content 

What is GEEK

Buddha Community

How to Fix PHP File_get_contents() Not Working

Hire PHP Developer

Looking to develop a PHP based website from scratch or revamp your existing website?

HourlyDeveloper.io has always been an industry leader for companies and business owners looking to hire PHP web developer. By choosing to Hire PHP Developer from our company, you can always expect the best results. Our PHP services and solutions are always flexible which means that no matter the nature of your project, you can always count on us for getting the best PHP expertise.

Consult with our experts: https://bit.ly/3aEGxPy

#hire php developer #php developer #php development company #php development services #php development #php

Hire PHP Developer - Best PHP Web Frameworks for Web Development

A framework that can drastically cut down the requirement to write original code to develop the web apps as per your requirement is PHP Framework. PHP frameworks offer code libraries for commonly used functions to reduce the development time.

Want to use PHP Web Frameworks for your web applications?

WebClues Infotech offers a service to hire dedicated PHP developers for all of the below-mentioned frameworks

  • Laravel Developer
  • Codeigniter Developer
  • Yii Developer
  • Zend Developer
  • Cake PHP Developer
  • Core PHP Developer

Not sure which framework to use for your PHP web application?

Contact us

Schedule Interview with PHP Developer https://bit.ly/3dsTWf0

Email: sales@webcluesinfotech.com

#hire php developer #hire php web developers #hire php developer in 2021 #hire php developers & dedicated php programmers #hire php developers india #hire and outsource freelance php developers

Hire Dedicated PHP Developer

Looking to hire affordable yet experienced PHP developers?

Hire Dedicated PHP Developer, who can convert your idea to reality, within the stipulated time frame. HourlyDeveloper.io expertise & experience as the top PHP development company put us above our competitors, in many ways. We have some of the top PHP developers in the industry, which can create anything you can imagine, that too, at the most competitive prices.

Consult with our experts:- https://bit.ly/2NpKnB8

#hire dedicated php developer #php developers #php development company #php development services #php development #php developer

Mariya James

Mariya James

1613990718

Top PHP web development company| PHP development services India

ValueCoders is a leading PHP app development company that focuses on building robust, secure & scalable web applications for start-ups, enterprises, and entrepreneurs.

We have 16+ years of experience and have delivered custom PHP web development solutions to 2500+ global clients catering industry verticals, including healthcare, adtech, eLearning, data analysis, Fintech, eCommerce, etc

Are you planning to outsource PHP development services? Or want to hire an offshore PHP development team?

#hire php developer #hire a php developer in india #hire dedicated php programmers #hire php coders #php developer in india #php developers for hire

Custom PHP Development Company | PHP Web Development Service

One programming language that has its root in Website development even at present is PHP Website Development. The PHP programming is executed on the server side which means it functions on web servers which helps the website in its performance.

Want to develop a website on PHP?

WebClues Infotech with its years of experience in Web Development helps individuals and businesses in launching a business website on PHP. The experienced development team with more than 20 years of experience is the solution to your every web development needs.

Want to know more about PHP website development?

Visit: https://www.webcluesinfotech.com/php-web-development/

Share your requirements https://www.webcluesinfotech.com/contact-us/

View Portfolio https://www.webcluesinfotech.com/portfolio/

#custom php development company #php web development service #php development services #php web development company india #php development services #hire php developers