1

I was wondering how on earth to get a json string into php.. here's my string:

(go here) http://urls.api.twitter.com/1/urls/count.json?url=http://www.onewiththem.com.au/&callback=twttr.receiveCount

and you should see this returned:

twttr.receiveCount({"count":0,"url":"http:\/\/www.onewiththem.com.au\/"});

I was wondering how to get the count number and have it set up in the variable $count ?

2
  • First of all, remove &callback=. Are you asking for a JSON parser? Commented Oct 3, 2012 at 2:16
  • use json_decode() php.net/manual/en/function.json-decode.php Commented Oct 3, 2012 at 2:18

2 Answers 2

2

Simple, using json_decode() and file_get_contents():

$data = json_decode(file_get_contents('http://urls.api.twitter.com/1/urls/count.json?url=http://www.onewiththem.com.au/'));
echo $data->count;

Note that I removed the &callback= from the URL, because that's only used for JSONP and PHP doesn't need it.

Sign up to request clarification or add additional context in comments.

Comments

0

If you mean get it in javascript, then the data in your callback function is the object. You could get the count just by data.count.

twttr.reciveCount = function (data) {
  console.log(data.count);
  // do the rest
}

If you call the api from php, then you should not use the callback parameter. Get the JSON response and then use json_decode to decode it. (Don't forget urlencode the url parameter.)

$response = file_get_contents('http://urls.api.twitter.com/1/urls/count.json?url='.urlencode('http://www.onewiththem.com.au/'));
$json = json_decode($response);
echo $json->count;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.