Jump to content

CURL


dreamwest

Recommended Posts

I never done anything with curl before.

 

Can someone give me an example of how i can get a file from remote server and store it in local server

 

http://remote_site.com/files/video.mpg

to

http://local_site.com/temp/video.mpg

 

		
$remote = "http://remote_site.com/files/video.mpg";
$local = "http://local_site.com/temp/video.mpg";


$ch = curl_init($remote);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);      
        curl_close($ch);
        echo $output;

 

Also can i use "http://" in the curl_int or do i have to remove slashes

Link to comment
Share on other sites

Firstly make sure the remote file is actually there. This code will output the remote file's header data:

 

<?php
$remote = 'URL to file here';
$ch = curl_init($remote);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.0; da; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10');
//return header only
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'HEAD');
$content = curl_exec($ch);
curl_close($ch);
echo $content;
?>

Let me see your output of that script, when you use the real URL.

Link to comment
Share on other sites

Ok, seems fine. Try the script below then (I added some error handling):

 

<?php
$remote = 'URL to file here';
$ch = curl_init($remote);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.0; da; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$content = curl_exec($ch);
if ($content === false) {
echo 'cURL returned false.';
}
curl_close($ch);
$path = 'where/to/put/the/file.mpg';
$saved = file_put_contents($path, $content);
if ($saved === false) {
echo 'No data was stored.';
} else {
echo "$saved bytes were stored in $path";
}
?>

I see the file is ~ 1.35 MB in size, so it shouldn't be a memory/timeout problem. But to be sure, you can check the server's memory limit and max execution time with this:

 

<?php
echo 'Memory limit: ' . ini_get('memory_limit') . '<br />Max execution time: ' . ini_get('max_execution_time');
?>

Link to comment
Share on other sites

thebadbad, script looks fine, but have you tried it without cURL ?

it sometimes works and its very simple

$remote = "http://remote_site.com/files/video.mpg";
$local = "path/to/storage/video.mpg";
file_put_contents($remote, $local);

 

Also remember you need write access to the folder your storeing the file to.!

Link to comment
Share on other sites

thebadbad, script looks fine, but have you tried it without cURL ?

it sometimes works and its very simple

$remote = "http://remote_site.com/files/video.mpg";
$local = "path/to/storage/video.mpg";
file_put_contents($remote, $local);

 

Also remember you need write access to the folder your storeing the file to.!

 

Nope doesnt work. I need to transfer it anyways to convert it to another format

Link to comment
Share on other sites

Ok, seems fine. Try the script below then (I added some error handling):

 

<?php
$remote = 'URL to file here';
$ch = curl_init($remote);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.0; da; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$content = curl_exec($ch);
if ($content === false) {
echo 'cURL returned false.';
}
curl_close($ch);
$path = 'where/to/put/the/file.mpg';
$saved = file_put_contents($path, $content);
if ($saved === false) {
echo 'No data was stored.';
} else {
echo "$saved bytes were stored in $path";
}
?>

I see the file is ~ 1.35 MB in size, so it shouldn't be a memory/timeout problem. But to be sure, you can check the server's memory limit and max execution time with this:

 

<?php
echo 'Memory limit: ' . ini_get('memory_limit') . '<br />Max execution time: ' . ini_get('max_execution_time');
?>

 

Hey it works!  Thanks

Link to comment
Share on other sites

Great. Maybe the remote site checks the user agent string, to disallow requests from PHP scripts, where the default user agent string hasn't been changed from 'PHP'. And MacTechie is right (even though his script isn't); it could be coded simpler using file_get_contents():

 

<?php
ini_set('user_agent', 'Mozilla/5.0 (Windows; U; Windows NT 6.0; da; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10');
$url = 'http://example.com/video.mpg';
$path = 'where/to/put/the/file/' . basename($url); //remote filename is used
file_put_contents($path, file_get_contents($url));
?>

Just requires allow_url_fopen to be set to On.

Link to comment
Share on other sites

And MacTechie is right (even though his script isn't); it could be coded simpler using file_get_contents():

Great just when i thought no one noticed lol, but i think everyone knew what i was getting at,

 

however if you want to convert the file, then surly your need to download it first then convert then remove old one!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.