dreamwest Posted May 16, 2009 Share Posted May 16, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/158365-curl/ Share on other sites More sharing options...
thebadbad Posted May 16, 2009 Share Posted May 16, 2009 Instead of echoing the file contents, save it with e.g. file_put_contents(): file_put_contents('/temp/video.mpg', $output); Quote Link to comment https://forums.phpfreaks.com/topic/158365-curl/#findComment-835184 Share on other sites More sharing options...
dreamwest Posted May 16, 2009 Author Share Posted May 16, 2009 Ive tried everything to get this to work.....i give up. Onwards to scriptlance............. Quote Link to comment https://forums.phpfreaks.com/topic/158365-curl/#findComment-835197 Share on other sites More sharing options...
thebadbad Posted May 16, 2009 Share Posted May 16, 2009 How is it not working? Do you get any error messages? What version of PHP are you using? Quote Link to comment https://forums.phpfreaks.com/topic/158365-curl/#findComment-835205 Share on other sites More sharing options...
dreamwest Posted May 16, 2009 Author Share Posted May 16, 2009 I have no idea what im doing with this. No errors, file wont go into local folder. Im using version 5 of php Is there some error handler i can use to see if it transfered correctly Quote Link to comment https://forums.phpfreaks.com/topic/158365-curl/#findComment-835208 Share on other sites More sharing options...
thebadbad Posted May 16, 2009 Share Posted May 16, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/158365-curl/#findComment-835209 Share on other sites More sharing options...
dreamwest Posted May 16, 2009 Author Share Posted May 16, 2009 echos out HTTP/1.1 200 OK Date: Sat, 16 May 2009 11:52:43 GMT Server: Apache Last-Modified: Tue, 17 Feb 2009 11:00:33 GMT Accept-Ranges: bytes Content-Length: 1350504 Content-Type: text/plain The file is video Quote Link to comment https://forums.phpfreaks.com/topic/158365-curl/#findComment-835211 Share on other sites More sharing options...
thebadbad Posted May 16, 2009 Share Posted May 16, 2009 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'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/158365-curl/#findComment-835225 Share on other sites More sharing options...
MadTechie Posted May 16, 2009 Share Posted May 16, 2009 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.! Quote Link to comment https://forums.phpfreaks.com/topic/158365-curl/#findComment-835235 Share on other sites More sharing options...
dreamwest Posted May 16, 2009 Author Share Posted May 16, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/158365-curl/#findComment-835243 Share on other sites More sharing options...
dreamwest Posted May 16, 2009 Author Share Posted May 16, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/158365-curl/#findComment-835244 Share on other sites More sharing options...
thebadbad Posted May 16, 2009 Share Posted May 16, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/158365-curl/#findComment-835248 Share on other sites More sharing options...
MadTechie Posted May 16, 2009 Share Posted May 16, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/158365-curl/#findComment-835494 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.