henshall / php-tinder-api
允许用户访问Tinder的私有API - 您可以使用短信进行身份验证,对用户进行滑动,更改位置等!
Requires
- guzzlehttp/guzzle: >=6.0
README
版本:1.0.0
这是一个用于Tinder(Match Group, Inc.)API的PHP包。
Tinder API是私有的 - 在互联网上关于如何访问或使用它的信息并不多。我使用了Chrome的网络标签来检查Tinder的xhr请求以创建此API - 它包含一些在互联网上其他地方找不到的方法,包括版本3的短信身份验证请求(在写作的最后一个月新增)。
此外,您将找到许多示例以及使用技巧、常见错误和更多信息。
此包中的类
Tinder API:Tinder API直接从Tinder API返回json解码后的结果,没有任何修改。一些方法可能需要偶尔更新,但我在过去几年中发现它们保持不变。
Tinder Mock Api:您可以使用'Tinder Mock API'类进行测试或开发,因为Tinder对每小时请求量有严格限制。您不希望与真实数据打交道,所以我提前创建了此MockAPI,并在下面提供了如何使用它的示例。
Tinder 接口:该接口确保我们在两个类中都有相同的方法,您可以将它注入到代码中以帮助解耦API/MockAPI和您的代码。下面有一些示例。
使用Composer安装
composer require henshall/php-tinder-api
先决条件
- 创建Tinder账户 (https://tinder.com/)
用法
确保在项目中包含该包
use Henshall\TinderApi; use Henshall\TinderMockApi; use Henshall\TinderApiInterface;
身份验证步骤 1(向您的手机发送短信进行验证)
在此步骤中,我们将收到一条短信,这是与我们Tinder账户关联的手机。
$myTinderPhoneNumber = "1112223456"; // no spaces $tinderApi = new TinderApi; $result = $tinderApi->requestCode($myTinderPhoneNumber);
身份验证步骤 2(将代码返回给Tinder并指定接收短信的手机号码)
收到代码后,我们将其发送回Tinder并获得刷新令牌。我们将使用此令牌获取真实的令牌,该令牌将在一段时间后过期。
$myTinderPhoneNumber = "1112223456"; // no spaces $tinderApi = new TinderApi; $result = $tinderApi->validateCode($myTinderPhoneNumber, "123456"); // Grab the Refresh Token $refresh_token = $result->data["refresh_token"];
身份验证步骤 3(请求Tinder令牌)
在这里我们可以请求我们的Tinder令牌,我们将在整个方法中使用它。如果Tinder令牌过期,您可以使用刷新令牌简单生成一个新的。
$tinderApi = new TinderApi; $result = $tinderApi->getTokenFromRefreshToken($refresh_token); // Grab the Tinder API Token $tinder_token = $result["api_token"];
获取配置文件
这获取了当前用户的配置文件,包括简介、图片、年龄、性别等。
$tinderApi = new TinderApi; $result = $tinderApi->getProfile($tinder_token);
获取元数据
这获取了当前用户的元数据,包括用户在Tinder横幅中看到的内容、用户设置等。
$tinderApi = new TinderApi; $result = $tinderApi->getMetadata($tinder_token);
获取推荐
这为您提供了您可以滑动的一组其他用户,并基于您的配置文件推荐给他们。它返回0-25个潜在匹配。
$tinderApi = new TinderApi; $result = $tinderApi->getRecommendations($tinder_token);
喜欢(滑动右)
这喜欢另一个用户(即向他们滑动右)。您可以在上面的getRecommendations方法中找到用户的ID。
$tinderApi = new TinderApi; $result = $tinderApi->like($tinder_token, $id);
忽略(滑动左)
这忽略另一个用户(即向他们滑动右)并忽略他们。您可以在上面的getRecommendations方法中找到用户的ID。
$tinderApi = new TinderApi; $result = $tinderApi->pass($tinder_token, $id);
更改位置
用户可以使用此方法更改位置。在这里我们尝试更新用户的滑动位置。如果失败,我们将简单地返回false而不是抛出错误。
try { // Asign Variables $tinderApi = new TinderApi; $token = "1234567890qwerty"; $position = ['lat' => "43.47876072885527", 'lon' => "-110.76437540803676"]; // Try To Update Location echo "Attempting to update user Swipe Location\n"; $update = $tinderApi->ping($token, $position); if (isset($update["status"]) && $update["status"] == 200) { echo "Location Change Successful! \n"; return $update; } else { echo "Location Change Failed \n"; return false; } } catch (\Exception $e) { echo "Location Change Failed \n"; return false; }
使用Tinder Mock API
Tinder 模拟 API 将为 TinderApi 中所有相同的方法返回成功的模拟响应。例如,您可以通过 TinderMockApi 的 getProfile 方法查看示例,这个示例与 TinderApi 的示例是否相同。
$tinderApi = new TinderMockApi; $result = $tinderApi->getProfile($tinder_token);
使用 TinderApiInterface
TinderApi 和 TinderMockApi 都实现了 TinderApiInterface。这不仅确保它们具有相同的方法,而且还允许我们在测试目的下解耦我们的代码。以下示例来自一个 Laravel 项目,其中在构造过程中注入了接口。这是一个“滑动”类,负责使用 Tinder API 进行滑动。
class Swiper { private $tinderApi; public function __construct(\App\Tinder\TinderApiInterface $tinderApi) { echo "SWIPER CREATED \n"; $this->tinderApi = $tinderApi; } public function swipe($token, $id, $swipe_type){ if ($swipe_type == "like") { try { echo "swiping right \n"; $response = $this->tinderApi->like($token, $id); } catch (\Exception $e) { echo "Failed to Swipe Right \n"; return false; } } elseif ($swipe_type == "pass") { try { echo "swiping left \n"; $response = $this->tinderApi->pass($token, $id); } catch (\Exception $e) { echo "Failed to Swipe Right \n"; return false; } } else { echo "swipe_type is wrong - you should never see this \n"; return null; } return $response; } }
在生产环境中,我们可以如下使用滑动类
$swiper = new Swiper(new TinderApi);
在测试时,我们可以如下使用这个类
$swiper = new Swiper(new TinderMockApi);