Jump to content

Retrieve part of URL?


Graxeon

Recommended Posts

I'm trying to make a script that will do this:

 

1. Go to the URL "site.com/1234"

**That URL redirects me to "site.com/image.php?num=25&loc=china"

2. Retrieves what comes after ".php?num=" and echos it to the user. In this case, it would echo "25"

 

I don't know if it's possible with $_GET because that would mean that the "num=25" was actually sent to the script, not the other way around.

 

I think it's possible with cURL and I have this code that someone else wrote but I don't know how to use it or what it does exactly.

 

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURN_TRANSFER, FALSE);
curl_exec($curl);

$url = curl_getinfo($curl, CURLINFO_EFFECTIVE_URL);
$query = parse_url($url, PHP_URL_QUERY);

 

Help please?

 

 

(note: I made a new topic because my first one seemed to scare people away with all of the replies, lol)

Link to comment
https://forums.phpfreaks.com/topic/202770-retrieve-part-of-url/
Share on other sites

I don't know if it's possible with $_GET because that would mean that the "num=25" was actually sent to the script, not the other way around.

 

Try it.

 

e.g

header('Location: http://site.com/image.php?num=25&loc=china');

 

on that page, simply call the values using $_GET

I can't change the coding of "http://site.com/image.php?num=25&loc=china"

 

And..."site.com/1234" links to different "num" values if "site.com/1234" is changed to say..."site.com/4321".

 

That's why I need a way of retrieving the "num" from any URL that I put in.

 

Edit:

 

so the script would be something like this:

 

site.com/script.php?url=site.com/1234

-"Execute 'site.com/1234'"

***..."redirects to site.com/image.php?num25&loc=china"

-"echo $_GET['num'] from site.com/image.php?num=25&loc=china"

I am not sure if this is relevant or not, but if site.com/1234 redirects, you may need this in the setopt:

 

CURLOPT_FOLLOWLOCATION

 

For it to follow the location, then at the final location you should be able to pull the get data. I have never tried it, and I could be mistaken, but yea.

If you want to do this using cURL: you can make a request to the URL (e.g. site.com/1234; be sure to use appropriate cURL options, like follow location)  then use curl_getinfo with CURLINFO_EFFECTIVE_URL (aside: curl_getinfo returns lots of useful information) to get the URL that the remote page redirected to.  Then, to access the part(s) of the URL that you need, you can use parse_url and parse_str.

 

P.S. The only thing difficult is working out what you really want from the descriptions given.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.