Get user info information from Google Plus API with PHP [duplicate] - php

This question already has answers here:
get json using php
(3 answers)
Closed 9 years ago.
When you're visiting this page:
https://www.googleapis.com/plus/v1/activities/z12gtjhq3qn2xxl2o224exwiqruvtda0i?key=AIzaSyAwnz3yjIcvsosfbudkzl9oogGrT21m6Ns
...I guess what is called an object, appears with a lot of information. But when I try to do a simple GET call and then print it, like so:
$tweets = $_GET['https://www.googleapis.com/plus/v1/activities/z12gtjhq3qn2xxl2o224exwiqruvtda0i?key=AIzaSyAwnz3yjIcvsosfbudkzl9oogGrT21m6Ns'];
print_r($tweets);
...It returns nothing... Why is that?

The $_GET super global is populated with the query string of the current request, it's not used to get stuff from the interwebs.
You're looking for file_get_contents():
$url = 'https://www.googleapis.com/plus/v1/activities/z12gtjhq3qn2xxl2o224exwiqruvtda0i?key=AIzaSyAwnz3yjIcvsosfbudkzl9oogGrT21m6Ns';
$tweets = file_get_contents($url);
print_r($tweets);
If that contains a JSON encoded response you need to additionally use json_decode() to use it.

Do you know how to use $_GET?
Check PHP documentation, you must have made a mistake, or you're using it the wrong way.

Related

Read Json with php, some troubles [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 5 years ago.
I have tried, checked many previous topics, and i cant find the way.
Parsing JSON with PHP
There is the answer, but im blind; actually, im a medimu PHP programmer, but im new with Json.
Here is my Json:
{"result":"true","disponible":{"aaa":"0.00001362","bbb":"0.000392","ccc":"0.00788523","ddd":"0.00004443","eee":"0.0001755","fff":"0.1755","ggg":"797.64618376"}}
My code:
$balances = json_encode(get_balances(),true);
print_r($balances);
The screen show my Json, so everything is ok here. Now, i want take the bolded values from the json and assign it to PHP variables.
$variable1 = $balances["disponible"]["bbb"];
$variable2 = $balances["disponible"]["ggg"];
echo "Valor 1: ".$variable1 ."<br>";
echo "Valor 2: ";$variable2 ;
But it dont work. I tried with many combinantions and nothing.
What im doing wrong?
Thanks a lot in advance. Im blocked with this.
Replace: $balances = json_encode(get_balances(),true); with: $balances = json_decode(get_balances(),true); if you are trying to get associative array.

can't access object in array PHP [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 6 years ago.
Have tried every variation I can think of to access an object property in an array. I'm getting some data back form an API which i'm storing in a variable called $userTokenValid:
$userTokenValid = [{"authTokenValid":1}];
i'm then trying to access the authTokenValid property like so:
echo json_decode($userTokenValid[0]->authTokenValid);
I appreciate this might be quite basic but can't spot where I have gone wrong.
$userTokenValid isn't valid php. However [{"authTokenValid":1}] is a valid json string.
$userTokenValid = '[{"authTokenValid":1}]';
you can decode it with
$json = json_decode($userTokenValid);
finally
echo $json[0]->authTokenValid;

Login to PayPal using CURL without using PayPal API [duplicate]

This question already has answers here:
PHP "php://input" vs $_POST
(6 answers)
Closed 6 years ago.
I want know,from where should I provide username, password credential topher's code... I know it is some what silly question. I am not getting
$raw_post_data = file_get_contents('php://input');
how exactly works
Please help me...
Thanks in advance.. :)
Thanks for asking this. I learned something new today.
This was answered in another post. You are actually doing the same as reading $HTTP_RAW_POST_DATA (wich is deprecated).
Usually when just submitting a basic form you want to read $_POST to get an array of elements php already splitted neatly for you:
$_POST = array(
"key1" => "value1",
"key2" => "value2",);
But when you actually need key1=value1&key2=value2 you want to get the "raw post data". Thats what file_get_contents('php://input'); does.
file_gets_content is usually intended to read a file (local file or url).

Extract site link from google URL [duplicate]

This question already has answers here:
How can I get parameters from a URL string?
(12 answers)
Closed 8 years ago.
I want to extract site link from Google URL, I need an efficient way to do this,
I have extracted this, but i am not comfortable with that like,
$googleURL = "http://www.google.ca/local_url?dq=food+Toronto,+ON&q=https://plus.google.com/110334461338830338847/about%3Fgl%3DCA%26hl%3Den-CA&ved=0CHAQlQU&sa=X&ei=HzrCVNX-JqSzigb-94D4CQ&s=ANYYN7nQx_FiR1PuowDmXBi1oyfkI2MImg";
I want this
https://plus.google.com/110334461338830338847/
I have done this in a following way.
$first = current(explode("about", $googleURL)); // returns http://www.google.ca/local_url?dq=food+Toronto,+ON&q=https://plus.google.com/110334461338830338847/
and then,
$myLink = explode("&q=", $first);
echo $myLink[1]; // return my need => https://plus.google.com/110334461338830338847/
but there may be two "about" or "&q=" in a googleURL which can cause problem.
I know that, this googleURL will be redirected to my need, but I need that specific link for a purpose.
I guess that it is not really safe to parse that since google can change its implementation anytime.
However, if you want to get a parameter from a String url, this question covers it pretty well :
How to get parameters from a URL string?
$parts = parse_url($googleUrl);
parse_str($parts['query'], $query);
echo $query['q'];

How to parse JSONP with PHP [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Extract JSONP Resultset in PHP
I get the response in the following format. Im facing trouble on how to get inside "Plugin" variable and access other variables inside it.I used json_decode(), but i cant access the variables.
Plugin
(
{
"plugin_a":"abc",
"plugin_b":"abc",
"plugin_c":"abc"
}
)
I tried
$a = json_decode($json,true);
echo $a['plugin_a'];
I dont get any output.
echo var_dump($json); gives me
string 'Plugin({
"plugin_a":"abc",
"plugin_b":"abc",
"plugin_ce":"abc" })'
try substr();
http://sandbox.phpcode.eu/g/40c20.php
<?php
$json = substr('Plugin
(
{
"plugin_a":"abc",
"plugin_b":"abc",
"plugin_c":"abc"
}
)', 9, -1);
print_r(json_decode($json));
Perhaps this will work for you:
$data=array('plugin_a'=>'abc','plugin_b'=>'bcd','plugin_c'=>'cde');
$json='{"Plugin":'.json_encode($data).'}';
$a=json_decode($json,true);
echo $a['Plugin']['plugin_a'];
It appears as though the actual json array may not have integrity. If this solution doesn't fit, can you post the code that actually builds the json array?

Categories