Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the destination URL using cURL?

Tags:

html

http

php

curl

How can I get the destination URL using cURL when the HTTP status code is 302?

<?PHP $url = "http://www.ecs.soton.ac.uk/news/"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $html = curl_exec($ch); $status_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);  if($status_code=302 or $status_code=301){   $url = "";   // I want to to get the destination url } curl_close($ch); ?> 
like image 594
ahmed Avatar asked Sep 17 '09 13:09

ahmed


People also ask

How do I get my curl URL?

To make a GET request using Curl, run the curl command followed by the target URL. Curl automatically selects the HTTP GET request method unless you use the -X, --request, or -d command-line option.

How do I redirect a URL in curl?

Using the curl Command The -L option tells the curl command to follow the URL redirections. The -s option is for silent mode, which means the command should not output anything to the terminal, while the -o option provides the path that it should send the output to instead of sending it to stdout.

How can I get redirected URL in PHP?

php $url="http://libero-news.it.feedsportal.com/c/34068/f/618095/s/2e34796f/l/0L0Sliberoquotidiano0Bit0Cnews0C12735670CI0Esaggi0Eper0Ele0Eriforme0Ecostituzionali0EChiaccherano0Ee0Eascoltano0Bhtml/story01.htm"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ ...


1 Answers

You can use:

echo curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); 
like image 157
Tamik Soziev Avatar answered Sep 19 '22 13:09

Tamik Soziev