Is there any way to access files of Third party server using PHP?
Yes. You just fopen them if url_fopen is enabled, or use CURL.
The easiest way - assuming url_fopen_wrappers are enabled - is simply using file_get_contents() with a remote (http://, ftp://) URL.
If you don't want to rely on them being enabled, use CURL - while it requires a PHP extension it's pretty common so chances are high it's enabled even on shared hosting.
Here are examples for both methods:
// using url_fopen_wrappers
$contents = file_get_contents('http://stackoverflow.com');
// using CURL
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://stackoverflow.com');
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$contents = curl_exec($curl);
curl_close($curl);
you could even use copy('thirdPartyFileUrl', 'fileO')
Related
I am trying to load an XML file, but for some reason I can't use a URL.
allow_url_fopen is set to On.
I used this, which didn't work:
$xml = simplexml_load_file("https://example.corn/test.xml");
But this does work:
$xml = simplexml_load_file("../test.xml");
I also tried file_get_contents:
$xml = file_get_contents("https://example.corn/test.xml");
and even cURL:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://example.corn/test.xml");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$xml = curl_exec($ch);
curl_close($ch);
For each example I am displaying the output with var_dump($xml);, which always shows bool(false) when using a URL and shows the correct XML data when using a local path.
I would just use the local path, but this is just for a test and the actual XML file I am going to fetch is from a different site.
I tested it on a local server and it worked, but on my hosted server, it doesn't work at all. They are using the same php.ini. My hosted server is using php-fpm, I'm unsure if that could be a factor, or if there is something that has to be changed with it.
This might be a configuration error that I am unaware of, because the code should work, and the link definitley exists, because I can click on it and see the correct xml in the browser.
What could be causing this issue?
Turns out that I had my openssl.cafile property set to some location that didn't exist.
For some reason no errors were showing when using the commands above.
I think the specified url is not valid or doesn't exist
Read about curl in the PHP docs
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
I installed localhost (xammp, wampserver) on a VDS. When I try to get a file using PHP Curl and file_get_contents, the download speed is very low. I can download a 100mb file in 10 minutes. If I try to download the same file with a browser, the duration is only 3 seconds. What can be the reason?
Thanks for your interest.
Downloading content at a specific URL is common practice on the internet, especially due to increased usage of web services and APIs offered by Amazon, Alexa, Digg, etc. PHP's cURL library, which often comes with default shared hosting configurations, allows web developers to complete this task.
u can try
/* gets the data from a URL */
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
The Usage
$returned_content = get_data('http://davidwalsh.name'); //something like this
Alternatively, you can use the file_get_contents function remotely, but many hosts don't allow this.
I have sites where stored some xml files, and I want to download to our server, we don't have ftp connection, so we can download through http. I always used file(url) is there any better way to download files through php
If you can access them via http, file() (which reads the file into an array) and file_get_contents() (which reads content into a string) are perfectly fine provided that the wrappers are enabled.
Using CURL could also be a nice option:
// create a new CURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.server.com/file.zip");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
set_time_limit(300); # 5 minutes for PHP
curl_setopt($ch, CURLOPT_TIMEOUT, 300); # and also for CURL
$outfile = fopen('/mysite/file.zip', 'wb');
curl_setopt($ch, CURLOPT_FILE, $outfile);
// grab file from URL
curl_exec($ch);
fclose($outfile);
// close CURL resource, and free up system resources
curl_close($ch);
I am trying to make a sort of license checking script, but as not all hosts allow curl_exec, I would like to know, if there is any alternative way of making a call-back?
This is how I do it with curl:
curl_setopt($ch, CURLOPT_URL, $url."/my_script.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $info);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_exec($ch);
You can simply use file_get_contents() with URLs:
file_get_contents('http://www.google.com');
This may be disabled, though, with just a simple INI option allow_url_fopen.
Alternatively, you may use fsockopen(), which should be available on most systems.
With fsockopen() you can open a socket to a HTTP server, then communicate using standard fwrite() and fread(). The downside is that you must write HTTP request headers by yourself, and you must also parse HTTP response headers too. If you look at fsockopen() on PHP's manual, you can see plenty of examples: http://fr2.php.net/fsockopen
My suggestion is to use cURL as primary option, file_get_contents() as secondary (if ini_get('allow_url_fopen') returns a positive result) and implement solutions likefsockopen() as fallback.
You could switch curl_multi_exec, but that's probably disabled as well.
Alternatively, use the HTTP stream wrapper. To configure a POST request, you'll need to set up your own HTTP context.
I need to retrieve a web content. I usually use wget but it's giving me an "Internal Server Error" this time. I also tried using file_get_contents(), it works when I'm using MAMP installed in my Mac, but when I run it in our server, it's not doing anything and prints no error message. Is there any other way to do this? Below are the code that I used.
<?php
echo "Retrieving Traffic Updates";
$source = file_get_contents('http://www4.honolulu.gov/hpdtraffic/MainPrograms/frmMain.asp?sSearch=All+Incidents&sSort=I_tTimeCreate');
echo $source;
?>
Thanks in advance!!
I don't know how to reply to all of you so I just added it here. CURL WORKED PERFECTLY. Thanks a lot guys.
I just have to ask though why file_get_contents() won't work even if it's enabled in the php.ini?
You can use cURL:
<?php
echo "Retrieving Traffic Updates";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www4.honolulu.gov/hpdtraffic/MainPrograms/frmMain.asp?sSearch=All+Incidents&sSort=I_tTimeCreate");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$source = curl_exec($ch);
curl_close($ch);
echo $source
?>
I would guess that you don't have the fopen wrappers enabled. To test, do:
var_dump(ini_get('allow_url_fopen'));
If this returns false (or 0, can't remember which), you can't use file_get_contents to open a remote file.
If cURL is installed, you can use that to access remote files.
allow_url_fopen may be turned off by your web host. You should see if this can be turned on. If not, cURL may still available so you should try using that instead.
If cURL is available to you, use that. You can determine if cURL is installed by doing a phpinfo();. For more information on cURL: PHP: cURL
Please reference http://davidwalsh.name/download-urls-content-php-curl for a solid example of using PHP's implementation of CURL to fetch the contents located at a given URL.
I believe you'll find the process fairly straightforward.
function curl($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
return curl_exec($ch);
curl_close ($ch);
}
$html = curl("http://www4.honolulu.gov/hpdtraffic/MainPrograms/frmMain.asp?sSearch=All+Incidents&sSort=I_tTimeCreate");