pass a variable to curl from get request php - php

<?php
#$test = $GET['link'];
$url = 'http://cf3.fancyimgs.com/310/20130521/367302239598940361_92b5e3190b3f.jpg';
$ch = curl_init($test);
$fc = fopen('c.jpg', 'w+');
curl_setopt($ch, CURLOPT_FILE, $fc);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fc);
?>
this is not working in codeigniter, above is my php code to get image from an url that i passed through GET
and am not getting the image from the get request url
can anyone help me to solve this.....
and below is the link from where i send the image url
click to download

It should be
$_GET['link']
^
|--- Note the underline
Not
$GET['link']
And
echo urlencode($url) // would be better approach ...

Related

PHP copy() doesn't work for random URLS which redirect to files

trying to copy() .MP3 file from remote url but it always fails.
$link = str_replace(' ','%20','http://mp3hungama.com/music/download.php?song_id=80522');
if (!copy($link,'/home2/muser/tmp/newname.mp3')) {
echo 'copy failed !';
}
$link url redirects to http://mp3hungama.com/music/audio//Indian%20Movies/Indian%20Movies%20Hindi%20Mp3%20Songs/Singh%20Is%20Bling%20(2015)/songs/Cinema%20Dekhe%20Mamma%20#%20Mp3HunGama.Com.mp3
same code works for others random urls like www.example.com/download.php?id=2332. what's the specifically problem here or any other way to do this job ?
I've tested your code and I also couldn't download the file, then, I've used curl an it work as expected:
$local_file = "/home2/muser/tmp/newname.mp3";//This is the file where we save the information
$remote_file = "http://mp3hungama.com/music/download.php?song_id=80522"; //Here is the file we are downloading
$ch = curl_init();
$fp = fopen ($local_file, 'w+');
$ch = curl_init($remote_file);
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_ENCODING, "");
curl_exec($ch);
curl_close($ch);
fclose($fp);
NOTE:
Make sure /home2/muser/tmp/ has write permissions.
TIP:
In the future, if you need to encode/decode a url, use urlencode or urldecode instead of str_replace
This link
already redirects to second link. So it's working already.

Why the header function is not working after CURL call?

Following is the call to an URL using CURL :
<?php
ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);
$link = $_GET['link'];
$url = "http://www.complexknot.com/user/verify/link_".$link."/";
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
?>
The variable $url contains one URL which I'm hitting using CURL.
The logic written in the file(present in a variable $url) is working absolutely fine.
After executing the code I want the control to be redirected to one URL. For it I've written following code :
header('Location: http://www.complexknot.com/login.php');
exit;
The following code is not working. The URL http://www.complexknot.com/login.php is not opening and a blank white page appears. This is the issue I'm facing.
If I don't use the CURL and hit the URL i.e. the URL contained in $url then it gets redirect to the URL http://www.complexknot.com/login.php that means header function works fine when I hit the URL in browser.
Why it's not working when I call it from CURL?
Please someone help me.
Thanks in advance.
This is happening because CURL is outputting the data. You must use curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); in order to let CURL returning data instead of outputting it.
<?php
ini_set('display_startup_errors', 0);
ini_set('display_errors', 0);
error_reporting(0);
$link = $_GET['link'];
$url = "http://www.complexknot.com/user/verify/link_$link/";
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
header('Location: http://www.complexknot.com/login.php');
You can use
<?php echo "<script>window.location.href = 'http://www.complexknot.com/login.php';</script>";die; ?>

How to grap this specific url?

The code below(learned from Save image from url with curl PHP) works fine when try to grap image from internet. But when come to the url below, I only got a "test.jpg" which is actually a 404 error page("test.jpg" could be opened by notepad). PS: And i could open the url with browser and could see the image. Thanks to Mike,Problem solved and code updated.
$url = 'https://spthumbnails.5min.com/10368406/518420256_c_570_411.jpg';
$reffer="http://www.sohu.com";
$user_agent="Baiduspider+(+http://www.baidu.com/search/spider.htm)";
$saveto="test.jpg";
grab_image($url,$saveto);
function grab_image($url,$saveto,$reffer,$user_agent){
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
curl_setopt($ch,CURLOPT_REFERER,$reffer);
curl_setopt($ch,CURLOPT_USERAGENT,$user_agent);
$raw=curl_exec($ch);
curl_close ($ch);
$fp = fopen($saveto,'w');
fwrite($fp, $raw);
fclose($fp);
}
Thanks to Mike. This site do need "CURLOPT_REFERER" option(which i ignored) to grap the image. And I also add the useragent option to make sure it work on other situation.

How to get last redirected url in php

I had used this code to get redirected url in $last_url string :
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "www.djgol.com/files/download/id/163799");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
$last_url= curl_getinfo($ch,CURLINFO_EFFECTIVE_URL);
$output = curl_exec($ch);
echo $last_url;
curl_close($ch);
?>
but i am unable to get redirected url ,, please help me
You're trying to get the URL before starting the request. Try moving the $last_url= curl_getinfo($ch,CURLINFO_EFFECTIVE_URL); line to after the curl_exec($ch); call.
use
$_SERVER['HTTP_REFERER'];
to get last url. I use this only.

How can get an image from a 301 redirect download link in PHP?

I'm trying to download this image with PHP to edit it with GD. I found many solutions for image links, but this one is a download link.
Edit:
$curl = curl_init("http://minecraft.net/skin/Notch.png");
$bin = curl_exec($curl);
curl_close($curl);
$img = #imagecreatefromstring($bin);
This is my current code. It displays "301 Moved Permanently". Are there CURLOPTs I have to set?
$curl = curl_init("http://minecraft.net/skin/Notch.png");
// Moved? Fear not, we'll chase it!
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
// Because you want the result as a string
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$bin = curl_exec($curl);
curl_close($curl);
$img = #imagecreatefromstring($bin);
Here is an option to directly save the image to a file (instead of using imagecreatefromstring):
<?php
$fileName = '/some/local/path/image.jpg';
$fileUrl = 'http://remote.server/download/link';
$ch = curl_init($fileUrl); // set the url to open and download
$fp = fopen($fileName, 'wb'); // open the local file pointer to save downloaded image
curl_setopt($ch, CURLOPT_FILE, $fp); // tell curl to save to the file pointer
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // tell curl to follow 30x redirects
curl_exec($ch); // fetch the image and save it with curl
curl_close($ch); // close curl
fclose($fp); // close the local file pointer
fopen - depends on your php settings if url fopen is allowed.
or curl
see the fine php manual.

Categories