Retreive access_token from url string [duplicate] - php

This question already has answers here:
Can I read the hash portion of the URL on my server-side application (PHP, Ruby, Python, etc.)?
(12 answers)
Closed 8 years ago.
I am using google api and in url i am getting like this:
http://website.com/generate-token/#access_token=ya29.AHES6ZTJwb9lzb0lua81oHa47-8ImcJf-8qE-02kIn8JcgEv&token_type=Bearer&expires_in=3600
I tried:
$_SERVER['QUERY_STRING']
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
But what i got is http://website.com/generate-token/
How can i get either the complete url and use regex to get the access_token or any other way in which i can get it?

My two cents (since you tagged it with jQuery i suppose Javascript is a valid solution):
var url = 'http://website.com/generate-token/
#access_token=ya29.AHES6ZTJwb9lzb0lua81oHa47-8ImcJf-8qE-
02kIn8JcgEv&token_type=Bearer&expires_in=3600';
function getParameterByName(name, url) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&#]" + name + "=([^&#]*)"),
results = regex.exec(url);
return results == null ? "" :
decodeURIComponent(results[1].replace(/\+/g, " "));
}
alert(getParameterByName('access_token', url));
http://jsfiddle.net/yejwF/

Related

How to get a object from JSON to PHP [duplicate]

This question already has answers here:
How to access object properties with names like integers or invalid property names?
(7 answers)
Closed 2 years ago.
I need to take a img link (https://upload.wikimedia.org/wikipedia/en/5/51/Minecraft_cover.png) from this api https://en.wikipedia.org/w/api.php?action=query&prop=pageimages&format=json&piprop=original&titles=Minecraft&pilicense=any. How to do it?
I wrote code like this, but I can print :
$img_url = "https://en.wikipedia.org/w/api.php?action=query&prop=pageimages&format=json&piprop=original&titles=Minecraft&pilicense=any";
$img_url = str_replace(" ", "%20", $img_url);
$img = json_decode(file_get_contents($img_url));
print_r ($img);
But how to print only img source?
The simplest way would be to use the following.
echo $img->query->pages->{'27815578'}->original->source;
Where 27815578 is the Page ID

PHP get JSON content from url [duplicate]

This question already has answers here:
How to loop through PHP object with dynamic keys [duplicate]
(16 answers)
Closed 5 years ago.
I'm using picasaweb(google) API to get the profile picture by email address..
I get the output from the URL:
http://picasaweb.google.com/data/entry/api/user/adircohen#gmail.com?alt=json
How can I get the variable with the content:
https://lh3.googleusercontent.com/-9GSeL43L-A4/AAAAAAAAAAI/AAAAAAAAAAA/x8Uy6PTaS1o/s64-c/112285456748585606724.jpg
I've tried this, but it's not working:
$json = file_get_contents('http://picasaweb.google.com/data/entry/api/user/adircohen#gmail.com?alt=json');
$obj = json_decode($json);
echo $obj->gphoto->$thumbnail;
Getting content from URL using this function file_get_contents();
$page = file_get_contents("http://picasaweb.google.com/data/entry/api/user/adircohen#gmail.com?alt=json");
$ap = json_decode($page); // decode the json record
$ap = (array)$ap->entry; // convert object to array for further process
$ap = (array)$ap['gphoto$thumbnail']; // convert object to array for
getting exact output as image url
echo $ap['$t']; // display the output

PHP get the parameters that are put after the question mark in an url [duplicate]

This question already has answers here:
How can I get parameters from a URL string?
(12 answers)
Closed 6 years ago.
I have an url that I call in this form :
http://MyIP/MyServer/php_scripts/test_sp.php?param1=key&param2=xyz5
and I would like to retrieve the value of the 'param1' and 'param2' parameters. Up to now, I was not successful, because all I get is an empty value.
Sure that I am certainly doing something wrong there, but is it with the way I am calling the url or in my php code itself? Any help will be highly appreciated. Thanks a lot for reading me.
The code below is the contents of my 'test_sp.php' file :
<html>
<head>
</head>
<body>
<?php
mainProcess();
function mainProcess()
{
$mavalue1 = "";
$mavalue2 = "";
$parts = parse_url($url, PHP_URL_QUERY); // << It looks like I also have a problem with this line.
parse_str($query, $params);
$mavalue1 = $_GET['param1'];
$mavalue2 = $_GET['param2'];
echo "Valeur de mavalue1 : " . $mavalue1; // <<< This is where I'm getting the empty value
echo "Valeur de mavalue2 : " . $mavalue2; // <<< Same problem here
}
?>
</body>
</html>
The $_GET array is an array with all the arguments passed in the URL.
You can use $_GET['param1']; to get the value of "param1" in the URL.
Have a nice day.
$mavalue1 = $_GET['param1'];
$mavalue2 = $_GET['param2'];

Get full website URL? [duplicate]

This question already has answers here:
Get the full URL in PHP
(27 answers)
Closed 8 years ago.
I have a function which looks like this:
function fullURL(){
$domain = $_SERVER['SERVER_NAME'];
$path = $_SERVER['PHP_SELF'];
$raw_url = $domain.$path;
$url = substr($raw_url, 0, -4);
echo $url;
}
This generates an URL which looks like 127.0.0.1/aura/profile
But, what if I would use this on a page which uses a $_GET-tag?
Then, my URL won't pick it up.
For example I have news?id=1, when using the function above it just becomes 127.0.0.1/aura/news.
What do I need to add to the function to make it output 127.0.0.1/aura/news?id=(id)?
What about this?
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
Note that you could also do:
$actual_link = "http://".$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"];
Original answer: https://stackoverflow.com/a/6768831/3150271

How to create bitly shortened url from user's inputed text?

Begginer here, people. Could anybody suggest any kind of solution? I've an user inputed text.
First of all I check if the text has any urls:
$post = preg_replace('/https?:\/\/[\w\-\.!~?&+\*\'"(),\/]+/','<a class="post_link"
href="$0">$0</a>',$post);
And after that I need to retrieve that url and put as a variable($url) to this function:
$short=make_bitly_url('$url','o_6sgltp5sq4as','R_f5212f1asdads1cee780eed00d2f1bd2fd794f','xml');
And finally, echo both url and user's text. Thanks in advance for ideas and critiques.
I've tried something like that:
$post = preg_replace('/https?:\/\/[\w\-\.!~?&+\*\'"(),\/]+/e',$url,$post){
$shorten = make_bitly_url($url,'o_6sgltpmm5sq4','R_f5212f11cee780ekked00d2f1bd2fd794f','json');
return '<a class="post_link" href="$shorten">$shorten</a>';
};
But even for me it looks some kind of nonsense.
Bitly does have an API available for use. You should check out API Documentation
Here's how to use the bit.ly API from PHP:
/* make a URL small */
function make_bitly_url($url,$login,$appkey,$format = 'xml',$version = '2.0.1')
{
//create the URL
$bitly = 'http://api.bit.ly/shorten?version='.$version.'&longUrl='.urlencode($url).'&login='.$login.'&apiKey='.$appkey.'&format='.$format;
//get the url
//could also use cURL here
$response = file_get_contents($bitly);
//parse depending on desired format
if(strtolower($format) == 'json')
{
$json = #json_decode($response,true);
return $json['results'][$url]['shortUrl'];
}
else //xml
{
$xml = simplexml_load_string($response);
return 'http://bit.ly/'.$xml->results->nodeKeyVal->hash;
}
}
/* usage */
$short = make_bitly_url('http://davidwalsh.name','davidwalshblog','R_96acc320c5c423e4f5192e006ff24980','json');
echo 'The short URL is: '.$short;
// returns: http://bit.ly/11Owun
Source: David Walsh article
HOWEVER, if you wanted to create your own URL shortening system (similar to bit.ly -- and surprisingly easy to do), here is an 8-part tutorial from PHPacademy on how to do that:
Difficulty level: beginner / intermediate
Each video is approx ten minutes.
Part 1
Part 2
Part 3
Part 4
Part 5
Part 6
Part 7
Part 8

Categories