我正试图从foursquare中提取我的签到源并将它们保存到MySQL数据库中。
我已经授权该应用并将其发送给我的令牌拉出来。我删除了代码中的那一步,因为我的印象是,一旦我获得了令牌,它就永不过期。但令牌在几个小时后停止工作。这是代码,它可以很好地工作,直到令牌发生了什么。
我错了吗?令牌真的到期了吗?我想设置一个cron作业来每小时运行一次这个脚本,但如果我必须每隔一段时间重新授权,我就不能这样做。
有什么想法吗?谢谢!
<?php
require_once('src/FoursquareAPI.class.php');
$dbh = new PDO('mysql:host=localhost;dbname=nick', 'root', 'root');
$client_id = 'CGOHPPM4BYUCXMDI3AQGHJ6453DN5DFKNJOCIZWDSNZMVM5I';
$secret = 'TT1YRJHW2BXR2CL54JW3R0DVL5HTC5ZTLCCSTLVPUYRWRFUE';
$redirect_url = "https://test/api/foursquare/cache_foursquare.php";
$foursquare = new FoursquareAPI($client_id, $secret);
$token = $foursquare->GetToken($code, $redirect_url);
$foursquare->SetAccessToken('BODGK0JQ1SDPFFH51FV4MFR1OBK0XILGF2GLEY1C2J0L4B2A');
$params = array("user_id"=>"self","limit"=>"250");
$endpoint = "users/self/checkins";
$response = $foursquare->GetPrivate($endpoint,$params);
$checkins = json_decode($response);
$checkins = $checkins->response->checkins->items;
try {
$dbh = new PDO('mysql:host=localhost;dbname=nick', 'root', 'root');
$sql = 'INSERT INTO foursquare (foursquare_id, checkin_date, venue_id, venue_name, venue_address, venue_city, venue_state,
venue_country, venue_zip, venue_lat, venue_lng, venue_category, icon, venue_url, been_here, shout)
VALUES (:foursquare_id, :checkin_date, :venue_id, :venue_name, :venue_address, :venue_city, :venue_state,
:venue_country, :venue_zip, :venue_lat, :venue_lng, :venue_category, :icon, :venue_url, :been_here, :shout)';
$stmt = $dbh->prepare($sql);
foreach( array_reverse($checkins) as $checkin) {
if (checkId($checkin->id)==0) {
$stmt->execute(array(
':foursquare_id' => $checkin->id,
':checkin_date' => $checkin->createdAt,
':venue_id' => $checkin->venue->id,
':venue_name' => $checkin->venue->name,
':venue_address' => $checkin->venue->location->address,
':venue_city' => $checkin->venue->location->city,
':venue_state' => $checkin->venue->location->state,
':venue_country' => $checkin->venue->location->country,
':venue_zip' => $checkin->venue->location->postalCode,
':venue_lat' => $checkin->venue->location->lat,
':venue_lng' => $checkin->venue->location->lng,
':venue_category' => $checkin->venue->categories[0]->name,
':icon' => $checkin->venue->categories[0]->icon->prefix.'32'.$checkin->venue->categories[0]->icon->suffix,
':venue_url' => $checkin->venue->url,
':been_here' => $checkin->venue->beenHere->count,
':shout' => $checkin->shout
)
);
echo "<ul>";
echo "<li>". $checkin->id . "</li>";
echo "<li>". $checkin->createdAt . "</li>";
echo "<li>". $checkin->venue->id . "</li>";
echo "<li>". $checkin->venue->name . "</li>";
echo "<li>". $checkin->venue->location->address . "</li>";
echo "<li>". $checkin->venue->location->city . "</li>";
echo "<li>". $checkin->venue->location->state . "</li>";
echo "<li>". $checkin->venue->location->country . "</li>";
echo "<li>". $checkin->venue->location->postalCode . "</li>";
echo "<li>". $checkin->venue->location->lat . "</li>";
echo "<li>". $checkin->venue->location->lng . "</li>";
echo "<li>". $checkin->venue->categories[0]->name . "</li>";
echo "<li>". $checkin->venue->categories[0]->icon->prefix.'32'.$checkin->venue->categories[0]->icon->suffix ."</li>";
echo "<li>". $checkin->venue->url . "</li>";
echo "<li>". $checkin->venue->beenHere->count . "</li>";
echo "<li>". $checkin->shout . "</li>";
echo "</ul><hr/>";
}
}
} catch(PDOException $e) {
echo $e->getMessage();
}
?>