php-Fetch redirect URL from Google - php

This is a search URL generated by google:
https://www.google.com/url?rct=j&sa=t&url=http://EXAMPLE.com/example-Article/&ct=ga&cd=CAIyHjliOWMxZGQ0MzEzZDc1MWY6Y28uaW46ZW46R0I6Ug&usg=AFQjCNGoEabdV1jKNPGH8vNeiuoec_E2Iw
Now it contains a redirect URL.
http://EXAMPLE.com/example-Article/
How do I extract this URL from the above using PHP?

Solved this eventually. I used the parse_url and the parse_str functions:
$baseURL = "https://www.google.com/url?rct=j&sa=t&url=http://www.EXAMPLE.com/MY-ARTICLE-&ct=ga&cd=CAIyHjZkZjRjNDk2NTAwYWE1ZGU6Y28uaW46ZW46SU46Ug&usg=AFQjCNGT48irR-R0yx_PiiDqOHYW14bc1w"
$URLParse = parse_url($baseURL);
parse_str($URLParse['query'],$queryResult);
echo $queryResult['url'];

Related

Q: How to check the extension of an URL website?

I would like to know if there is a way to check the extension of an URL website ? For example, do something when the website is like http://example.es/ and do another thing when the website is like http://example.fr/
I have check that there is something like
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
which returns the current URL of the web page.
Thanks for help.
Use parse_url() function to get host part of the url then explode by . and get last element of an array
Example below:
$url = 'http://' . $_SERVER['SERVER_NAME'];
echo end(explode(".", parse_url($url, PHP_URL_HOST)));
// echos "com"
From your example I assume that you are using PHP, then you can use parse_url to get the components.
https://www.php.net/parse-url
For example you can get the host example.fr and example.com, then do explode on host string to get the tld, .fr or .com, which should help you to do further if-else.

Get custom URL PHP

I'm working on a simple app to retrieve a JSON file info, but to do so I need an ID, I fixed that problem by adding it to the URL like this:
http://localhost:8000/comic_library/comic.php?id=2097
Since this is not a friendly URL, I change so it looks like this:
http://localhost:8000/comic_library/comic/2097
But when I retrieve the URL, the only thing I got back is:
http://localhost:8000/comic_library/comic.php
But I need the id in order to retrieve the JSON file, is there a way of getting the custom URL and not just the absolute URL
As you are using core php, it is not possible for you to have an url like http://localhost:8000/comic_library/comic/2097, if you used a framework like codeigniter or cms like wordpress then you would get that url. Core php urls have .php extension.
You should Pass the URL like this http://localhost:8000/comic_library/comic.php/2097
$uri_path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$uri_segments = explode('/', $uri_path);
echo $uri_segments[2]; // for http://localhost:8000/comic_library/comic.php/2097 you will get '2097'
First split the URL segments and Get the desired segment array value.
if your url is this
http://localhost:8000/comic_library/comic/2097
you can get your id by the following process
$fullurl = 'http://localhost:8000/comic_library/comic/2097';
$exploded_url = explode('/',$fullurl);
echo $final_id = end($exploded_url);

How to get full url along with # symbol in php

I am using AngularJS routing with php,
so that my urls look like
http://localhost/admin/home#/categories
When ever I am trying to get url it wont gave full url. It gives only /admin/home.
I am using $_SERVER['REQUEST_URI'] and codeigniter segment. Both are not working.
Can any one help?
This information is not being past to the server I'm afraid.
The url part after # is only for browser use. You can add GET or POST parameters to pass this information to your server like:
http://localhost/admin/home?hash=categories#/categories
use:
$_SERVER[PHP_SELF]
It will give the full url, including everything.
If you want to strip stuff from the url, use things like this:
$url = trim(strtok("$url", '?'));
$url = str_replace("#!/", "", "$url");

Php Get Parameter with & symbol

I am sending a link in parameter to a php page like :
http:// exemple.com/test.php?url=http:// i-want-to-get-this-link.com
in php Page I get this link like this :
$_GET['url'];
the problem some time the link has & symbol :
http:// exemple.com/test.php?url=http:// i-want-to-get-this-link .com/?page1&desc/otherstaff
so I only get the first part of the link : http:// i-want-to-get-this-link.com/?page1
how could I get the rest of the link &desc/otherstaff
Use urlencode on your url variable to generate the url
<?php
$url = 'http:// exemple.com/test.php?url='.urlencode('http://i-want-to-get-this-link.com');
?>
EDIT : you do not need urldecode with $_GET and $_REQUEST variables.
and urldecode to get the original URL.
<?php
$url = urldecode($_GET['url']);
?>
When you create your whole url, try to urlencode it:
http://php.net/manual/en/function.urlencode.php
then you can use:
urldecode($_GET['url']);
You can use parse_url() function to get the query, if you cannot control the input url. Try this:
print_r(parse_url($_SERVER['REQUEST_URI']));

i want previous page URL with GET parameters

Currently i using this ,
$page=urlencode($_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);
which gives url without get parameters, but i need the previous page URL with GET parameters
Use HTTP_REFERER that referred the user agent to the current page
$page=urlencode($_SERVER['HTTP_REFERER']);
You have to use the Referrer header.
$_SERVER['HTTP_REFERER']
Please, keep in mind, a user can change the value of headers, so you can't "trust" it.
$url ="";
if (isset($_SERVER['HTTP_REFERER'])){
$url = $_SERVER['HTTP_REFERER'];
}
if want to send post get this as encryption the use
if (isset($_SERVER['HTTP_REFERER'])){
$url = urlencode($_SERVER['HTTP_REFERER']);
}
if you want to decrypt the url
$url = urldecode($url);
Simply used
$_SERVER['HTTP_REFERER']
You will get whole URL (with get parameters)

Categories