gilberg-vrn/php-foursquare

此包最新版本(1.1.8)没有提供许可证信息。

一个简单的PHP库,用于访问Foursquare API(见:https://developer.foursquare.com)

1.1.8 2015-04-21 08:40 UTC

This package is auto-updated.

Last update: 2024-09-19 02:47:27 UTC


README

我找不到一个封装了Foursquare API所有功能的PHP库,同时仍然可以快速集成到应用程序中。这个库旨在满足这一需求。

安装

安装Foursquare库的最佳方式是使用 Composer

composer require gilberg-vrn/php-foursquare:'1.1.*'

如果你没有使用自动加载器,你需要 require_once 自动加载文件

require_once 'vendor/autoload.php';

另一种安装方式是直接从本仓库下载最新版本。

使用

此库为php应用程序提供了包装,使其能够向Foursquare的公共和仅认证资源发出请求。此外,它还提供了封装从Foursquare获取用户 auth_token 所需主要方法的函数。您无需为调用API端点显式创建函数,可以直接通过路径调用单个端点(例如,venues/40a55d80f964a52020f31ee3/tips

查询API

$foursquare = new FoursquareApi("<your client key>", "<your client secret>");

// Searching for venues nearby Montreal, Quebec
$endpoint = "venues/search";
	
// Prepare parameters
$params = array("near"=>"Montreal, Quebec");

// Perform a request to a public resource
$response = $foursquare->GetPublic($endpoint,$params);

// Returns a list of Venues
// $POST defaults to false
$venues = $api->GetPublic($endpoint [,$params], $POST=false);
		
// Note: You don't need to add client_id, client_secret, or version to $params

// Setting the access token to enable private requests
// See examples/tokenrequest.php for how to retrieve this
$auth_token = "<your auth token>";
$foursquare->SetAccessToken($auth_token);

// Request a private endpoint (Requires Acting User)
$endpoint_private = "users/self";

// Returns a single user object
$me = $foursquare->GetPrivate($endpoint_private);
// Note: You don't need to add oauth_token to $params

用户认证(见 examples/tokenrequest.php

$foursquare = new FoursquareApi("<your client key>", "<your client secret>");

// Some real url that accepts a foursquare code (see examples/tokenrequest.php)
// This URL should match exactly the URL given in your foursquare developer account settings
$redirect_url = "https:///foursquare_code_handler";

// Generates an authentication link for you to display to your users
// (https://foursquare.com/oauth2/authenticate?...)
$auth_link = $foursquare->AuthenticationLink($redirect_url);
		
// Converts an authentication code (sent from foursquare to your $redirect_url) into an access token
// Use this on your $redirect_url page (see examples/tokenrequest.php for more)
$code = $_GET['code'];	

$token = $foursquare->GetToken($code, $redirect_url);

// again, redirect_url must match the one you set in your account exactly
// and here is where you would store the token for future usage

添加功能和测试

如果您想提交功能,请同时更新 tests/FoursquareApiTest.php 文件,并添加适当的单元测试 - 这将确保更改可以更快地被接受。

运行测试需要phpunit,可以按照以下方式运行

export FOURSQUARE_CLIENT_ID=<your client id>
export FOURSQUARE_CLIENT_SECRET=<your client secret>
export FOURSQUARE_TOKEN=<your access token>
phpunit --bootstrap src/FoursquareApi.class.php tests/FoursquareApiTest.php

技巧:获取测试用的访问令牌的最简单方法是在 API探索器中查找自己,并直接从下面的灰色url信息中获取(OAuth token 自动添加。 https://api.foursquare.com/v2/users/self?oauth_token=...) 在输入框下方。

还需要什么吗?

此库不处理您的令牌管理 - 只处理您的代码与Foursquare API之间的交互。如果您在使用需要通过 auth_token 使用用户数据的应用程序,您需要将令牌与库分开存储在会话中。