Alternative to file_get_contents?
Use cURL. This function is an alternative to file_get_contents
.
function url_get_contents ($Url) {
if (!function_exists('curl_init')){
die('CURL is not installed!');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
Is there any alternative for the function file_get_contents()?
file_get_contents() pretty much does the following:
$filename = "/usr/local/something.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
Since file_get_contents() is disabled, I'm pretty convinced the above won't work either though.
Depending on what you are trying to read, and in my experience hosts disable remote file reading usually, you might have other options. If you are trying to read remote files (over the network, ie http etc.) You could look into the cURL library functions
Alternative of file_get_contents function
In case you’re using PHP to retrieve data from a certain server you probably came across the problem that it may work for you but a client complained about lots of errors. It’s pretty likely that you’ve relied on the fact that allow_url_fopen
is set to true. This way you can put pretty much anything – local path or a URL – into function calls like include
or maybe simplexml_load_file
.
If you’d like to get around this problem you can advice your client to make the necessary changes in his php.ini
file. Most of the time this isn’t an option because the hosting company decided to disable this feature for security reasons. Since almost everybody got cURL installed we can use this to retrieve data from another web server.
Implementation
I’ll present a wrapper that helps you loading an XML file. It uses simplexml_load_file
if allow_url_fopen
is enabled. If this feature is disabled it employs simplexml_load_string
and cURL. If none of this works we’ll throw an exception because we weren’t able to load the data.
class XMLWrapper {
public function loadXML($url) {
if (ini_get('allow_url_fopen') == true) {
return $this->load_fopen($url);
} else if (function_exists('curl_init')) {
return $this->load_curl($url);
} else {
// Enable 'allow_url_fopen' or install cURL.
throw new Exception("Can't load data.");
}
}
private function load_fopen($url) {
return simplexml_load_file($url);
}
private function load_curl($url) {
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return simplexml_load_string($result);
}
}
//For Json
class JsonWrapper {
public function loadJSON($url) {
if (ini_get('allow_url_fopen') == true) {
return $this->load_fopen($url);
} else if (function_exists('curl_init')) {
return $this->load_curl($url);
} else {
// Enable 'allow_url_fopen' or install cURL.
throw new Exception("Can't load data.");
}
}
private function load_fopen($url) {
$raw = file_get_contents($url);
$data = json_decode($raw);
return $data;
}
private function load_curl($url) {
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
$data = json_decode($result);
return $data;
}
}
The code is pretty simple, create an instance of the given class and call the loadXML
method. It’ll call the right
private method which finally loads the XML. Loading some XML is just an example, you can use this technique with e.g. include
or require
too.
Alternate of file_get_contents function in WordPress
Try this code
function send_api_request($name,$email,$phone,$company,$message,$source){
$postData = array(
"AccessKey" => "xxxxx",
"Subject" => "",
"Name" => $name,
"Message" => $message,
"Phone" => $phone,
"Email" => $email,
"Company" => $company,
"SourceFrom" => $source
);
// Send the request
$response = "";
$response = wp_remote_post("http://someURL.com/api/lead", array(
'method' => 'POST',
'timeout' => 45,
'redirection' => 5,
'httpversion' => '1.0',
'blocking' => true,
'headers' => array("Content-Type" => "application/json"),
'body' => json_encode($postData),
'cookies' => array()
));
}
Faster alternative to file_get_contents()
There are many ways to solve this.
You could use cURL with its curl_multi_* functions to execute asynchronously the requests. Or use cURL the common way but using 1 as timeout limit, so it will request and return timeout, but the request will be executed.
If you don't have cURL installed, you could continue using file_get_contents but forking processes (not so cool, but works) using something like ZendX_Console_Process_Unix so you avoid the waiting between each request.
Whats the alternative for file_get_contents in Laravel?
I suggest guzzlehttp (Github Page)
Easy Usage:
Installation:
composer require guzzlehttp/guzzle
Send request and get response:
$client = new \GuzzleHttp\Client();
$res = $client->get($url);
$content = (string) $res->getBody();
Alternative to file_get_content (php://input)
Thank you very much Cristian. it worked.
I also found another way which is PEAR php Component that provides lots of functions which are alternative to main php functions for those people who are struggling like me!
for instance, in my case, I uploaded the file_get_content function to the same folder and included that in get.php
instead of
$content = file_get_contents('php://input');
file_put_contents("data.txt", $content);
I'm now using
require_once 'file_get_contents.php';
$content = php_compat_file_get_contents('php://input');
file_put_contents("data.txt", $content);
Related Topics
Laravel Querybuilder How to Use Like in Wherein Function
MySQL Table Does Not Exist Error, But It Does Exist
Curl - Load a Site with Cloudflare Protection
Xml Parser Error: Entity Not Defined
How to Execute PHP Code in a Sandbox from Within PHP
Efficient Way to Pass Variables from PHP to JavaScript
Confusion Between Bindvalue() and Bindparam()
Is This the Most Efficient Way to Get and Remove First Line in File
Certificate Error Using Imap in PHP
When to Wrap Curly Braces Around a Variable
Php7 with Apcu - Call to Undefined Function Apc_Fetch()
How to Check If a Longitude/Latitude Point Is Within a Range of Coordinates