hownowstephen/php-foursquare

此软件包最新版本(v1.2.0)没有可用的许可信息。

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

v1.2.0 2015-06-08 14:19 UTC

This package is not auto-updated.

Last update: 2024-09-10 07:02:06 UTC


README

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

安装

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

composer require hownowstephen/php-foursquare:'1.2.*'

如果您没有使用自动加载器,则需要包含自动加载文件

require_once 'vendor/autoload.php';

另一种安装方法是直接从本存储库下载最新版本。

用法

此库为PHP应用程序提供包装,以便对其公共和仅限认证的资源进行请求。此外,它还提供了封装从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 = $foursquare->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 = "http://localhost/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.php tests/FoursquareApiTest.php

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

我还需要什么?

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