6

I want use php curl with oauth to get the JSON data from twitter. Here is my code. return na error message "error":"Timestamp out of bounds".

I want to know, how to make a correct twitter api curl with oauth?

  1. what is oauth_consumer_key, oauth_token, oauth_nonce, oauth_signature? am I right?

  2. how to solve "error":"Timestamp out of bounds"?

  3. how about my curl_setopt method?

In many way, I would like to use this php curl to decode other twitter api(changed url). Thanks a lot.

$callback="<callback url>";
$consumer_key="<Consumer key>";
$consumer_secret="<Consumer secret>";
$oauth_token="<Access Token (oauth_token)>";
$oauth_signature="<Access Token Secret (oauth_token_secret)>";//these key word in my api panel
$time = mktime(date("Y-m-d H:i:s"))-86400;
$url = "https://api.twitter.com/1/friends/ids.json?";
$url .= "user_id=<user id>";
$url .= "&realm=".urlencode($callback)."";
$url .= "&service_provider_id=11";
$url .= "&oauth_consumer_key=".$consumer_key."";
$url .= "&oauth_token=".$oauth_token."";
$url .= "&oauth_nonce=".$consumer_secret."";
$url .= "&oauth_signature_method=HMAC-SHA1";
$url .= "&oauth_timestamp=".$time."";
$url .= "&oauth_version=1.0";
$url .= "&oauth_signature=".$oauth_signature."";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: api.twitter.com'));
$json = curl_exec($ch);
curl_close ($ch);
$data = json_decode($json, true);
print_r($json);
Rich
  • 5,603
  • 9
  • 39
  • 61
yuli chika
  • 9,053
  • 20
  • 75
  • 122
  • 1
    Not that I can see the error offhand, but creating a date from a format string, then converting it to a timestamp seems a bit wasteful. Why not just get the timestamp directly with `time();`? – Jeff Parker Jun 06 '11 at 09:27

2 Answers2

3

You're using mktime incorrectly, it doesn't accept a date string. Instead of that however, I'd recommend that you do ...

$time = time() - 86400;

With regards to your other questions, I recommend getting an oauth capable twitter library for PHP. There are plenty out there, and there's no point in reinventing the wheel ... usually :)

https://dev.twitter.com/docs/auth/oauth/single-user-with-examples

zourbuth
  • 898
  • 7
  • 16
Jeff Parker
  • 7,367
  • 1
  • 22
  • 25
  • still `"error":"Timestamp out of bounds"`. I will try `twitter library`. but I think it is too complex. I just want use an easy way to get the data which I need. Thanks. – yuli chika Jun 06 '11 at 09:39
  • If you continue to have trouble, provide us with an example request (minus the sensitive bits of course). I think you'll find adding a twitter library will save you a lot of time :) – Jeff Parker Jun 06 '11 at 09:46
  • so how to use `twitter library`? I just see all the articles about a `sign in` function. but how to get my request? Could u give me some code, that can `print_r` the json code(with which twitter library)? Thanks. – yuli chika Jun 06 '11 at 09:56
  • You could try this specific one. There's a suitable example on their homepage too. http://jdp.github.com/twitterlibphp/ – Jeff Parker Jun 06 '11 at 09:59
  • Also checkout `strtotime`, eg `$time = strtotime('-1 day')` . Its overkill for this situation, but good to know. – John Himmelman Jul 05 '11 at 21:32
1

Also, check that you're time/date are properly set on your server. Twitter will throw an out of bounds error if your timestamp is off by more than ~24 hours.

Guide for setting date/time on linux - http://www.cyberciti.biz/faq/howto-set-date-time-from-linux-command-prompt/

John Himmelman
  • 21,504
  • 22
  • 65
  • 80