Click here to Skip to main content
15,850,298 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

Using the twitter API twitteroauth, I retrieved credentials in json format as shown below:

{"oauth_token":"XXXXXXXXXXXXXXXXXXXXXXX","oauth_token_secret":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX","user_id":"123456789","screen_name":"myTwitterUsername"}

How would I use AJAX to get the user_id and screen_name from the JSON results above and store them into seperate variables in PHP so that they can be used on the UI?
Posted
Comments
Sergey Alexandrovich Kryukov 8-May-15 13:35pm    
Why Ajax? Haven't you already got the JSON string on the server side? Anyway, do you want to retrieve it on server side or client side?
—SA
jba1991 9-May-15 8:00am    
I have used json_encode on a variable and echo'd the variable which displays the data on the client side (it is displaying the line of code from the question in the browser).

I just want to be able to get each part of the json encoded data shown in the question and store them into their own variable, so I can echo something like "welcome, $sceen_name" and store it in a PHP session
jba1991 9-May-15 16:53pm    
Can anyone help?
Sergey Alexandrovich Kryukov 9-May-15 20:03pm    
And what, you did not even notice the answer of yesterday?
—SA

1 solution

The question is not clear enough, but it's possible that you just need PHP json_decode:
http://php.net/manual/en/function.json-decode.php[^].

—SA
 
Share this answer
 
Comments
jba1991 10-May-15 7:14am    
I just read this wrong as json_encode not json_decode, so apologies for that. I've tried json_decode($json) with $json being my json formatted data, but nothing changed.
jba1991 10-May-15 7:30am    
json_decode() expects parameter 1 to be string, array given

I am getting this error when using json_decode. I am aware that that json encoded data is in an array, how do I convert this to a string before decoding?
Sergey Alexandrovich Kryukov 10-May-15 10:49am    
The parameter should be the string, yes. In your, sample, this is not an array, this is some object which does not need deserialization.
—SA
jba1991 10-May-15 7:45am    
I currently have this value returning using var_dump(json_decode($json, true));

array(4) { ["oauth_token"]=> string(50) "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" ["oauth_token_secret"]=> string(45) "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" ["user_id"]=> string(10) "123456890" ["screen_name"]=> string(8) "username" }


Is this correct?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900