In my code when I try to fetch redirect using cURL, it won't show the new location but when I put the same URL in browser it redirects to somewhere else I don't know how without redirect rules. but I need to get that same URL in result.
Here is the example URL
http://www.dailymotion.com/cdn/H264-1280x720/video/x1r819x.mp4?auth=1505704620-2562-a9qmrjvz-6e0f4066eb3a57ce79c011f5c3f932f3
Here is the Redirect URL
https://proxy-23.sg1.dailymotion.com/video/907/091/106190709_mp4_h264_aac_hd.mp4?auth=1505539349-6658-b3dukan0-a41773a8fa2338e758ce74cff24b0390#cell=sg1&hls_heuristic=1&hls_startFragPrefetch=1
Here is my PHP Code
$url = "http://www.dailymotion.com/cdn/H264-1280x720/video/x1r819x.mp4?auth=1505704620-2562-a9qmrjvz-6e0f4066eb3a57ce79c011f5c3f932f3";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_NOBODY, true); // HTTP request is 'HEAD'
$headers=curl_exec($ch);
preg_match_all('/^Location:(.*)$/mi', $headers, $matches);
curl_close($ch);
$print_newurl = !empty($matches[1]) ? trim($matches[1][0]) : 'http://www.google.com.fr';
echo $print_newurl;
Try to follow the redirect with CURLOPT_FOLLOWLOCATION.
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
Edit:
$last_url = curl_getinfo($ch, CURLINFO_REDIRECT_URL);
If that doesn't work, then try with CURLINFO_EFFECTIVE_URL. See more http://php.net/manual/en/function.curl-getinfo.php
Related
If I try to curl this css file like this https://web.archive.org/web/20170322073013cs_/http://afterschoolprograms.com/sites/all/themes/afterschoolprograms_dev/style.css?n , it return 403 Forbidden error.
If I try to open it in browser it return the same error
If I open this page https://web.archive.org/web/20170322073013/http://afterschoolprograms.com/, this css page works fine and return 302 that lead to working css file.
Then If open the same css file again with browser, it works fine.
How can I always curl this css file and return the 302 redirection that lead to a working css file?
Here is the php code I m currently using and that always return 403 Forbidden error:
$url = "https://web.archive.org/web/20170322073013cs_/http://afterschoolprograms.com/sites/all/themes/afterschoolprograms_dev/style.css?n";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout in seconds
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow 301 redirection
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_USERAGENT,'waybackmachinedownloader');
$html = curl_exec($ch);
curl_close($ch);
As suggested by Cbroe, adding the referer header works :
<?php
$url = 'https://web.archive.org/web/20170322073013cs_/http://afterschoolprograms.com/sites/all/themes/afterschoolprograms_dev/style.css?n';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_REFERER, 'https://web.archive.org/web/20170322073013/http://afterschoolprograms.com/');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$html = curl_exec($ch);
curl_close($ch);
var_dump($html);
?>
I have a link: http://rstyle.me/n/bfj42hqgww
which redirects to https://www.missguided.co.uk/clothing/coats-jackets/double-breasted-tailored-long-faux-wool-coat-camel
How do I get the redirect link?
I tried the following:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Must be set to true so that PHP follows any "Location:" header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch); // $a will contain all headers
$url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); // This is what you need, it will return you the last effective URL
echo $url;
but it did not work. I also tried:
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE); // We'll parse redirect url from header.
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE); // We want to just get redirect url but not to follow it.
$response = curl_exec($ch);
preg_match_all('/^Location:(.*)$/mi', $response, $matches);
curl_close($ch);
echo !empty($matches[1]) ? trim($matches[1][0]) : 'No redirect found';
i don't have any knowledge about this but i am using in my website something like this for sending sms. so, you could try if it is workout.
$URL="example.com";
$ch = curl_init($URL);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
You got redirect not from server but from javascript. cURL doesn't execute javascript. When I open your link I saw that real link is hidden in a "title" tag.
$url = 'http://rstyle.me/n/bfj42hqgww';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
$data = curl_exec($curl);
curl_close($curl);
// Get real URL.
preg_match("|<title>(.+?)</title>|si", $data, $matches);
$link = ( isset($matches[1]) ) ? $matches[1] : '';
echo $link;
Hi i have a website say abc.com and there is a another site which is installed in the subfolder abc.com/site .When i am making a curl request from the abc.com/site to abc.com it is showing 302 .
below is my code
$ch = curl_init();
$data['username'] = 'abc';
$data['password'] = 'abc';
curl_setopt($ch, CURLOPT_URL, "abc.com/site ");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$a = curl_exec($ch);
print_r($a);
but when i am making the same request to the production site from the dev site it works well. It seems like it is not working on the same origin. Please suggest how can i fix this
302 is a redirect and you have explicitly told curl to not follow redirects with CURLOPT_FOLLOWLOCATION set to false...
I'm trying to load information from Wikipedia with cURL and PHP, but it's not working as intended.
See my code :
$url = "http://en.wikipedia.org/w/api.php?action=opensearch&format=xml&limit=1&search=stackoverflow";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, 'USERAGENT');
$result = curl_exec($ch);
curl_close($ch);
//return $result;
var_dump($result);
It doesn't return anything. However, if you load the address in your browser, it works!
So I'm wondering what should I change in order to execute this cURL request well.
Thank you folks !
The request is redirecting to https. So either reference the https uri instead or set CURLOPT_FOLLOWLOCATION to true:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
i want to get link from redirected by php click counter
http://www.mymaza.com/songs/pakistani_pop/download/pakistan.php?id=168
this page redirect to
http://sound5.mp3pk.com/pakistani/sajjad_ali/babia/babia1(www.songs.pk).mp3
i want to get redirected page link
However i tried other method seems it like not easy
You can use cURL to resolve a given URL:
function resolveURL($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
$response = curl_exec($ch);
$resolved = curl_getinfo($ch);
return $resolved['url'];
}
Usage:
echo resolveURL('http://example.com/someurl.php?foo=42')