社区/somin-api-sdk

SoMin API SDK是一个用于调用SoMin API的PHP库。

1.5.4 2018-11-26 05:19 UTC

This package is not auto-updated.

Last update: 2024-09-29 05:09:15 UTC


README

SoMin API SDK用于Somin API是一个PHP库,用于调用Somin API。该库允许用户从PHP 5.6或更高版本进行API调用。

Build Status

示例

$requester = new SimpleHttpRequester();
$authorizer = new CredentialsAuthorizer($requester);

$tokenRequest = (new UserCredential())
                        ->setUsername(USERNAME)
                        ->setPassword(PASSWORD);

// Call authorization method
$tokenResponse = $authorizer->auth($tokenRequest);

// Set bearer token to current HttpRequester
$requester->setBearer($tokenResponse->getToken());

$request = (new UserProfileData())
             ->setTexts([
                 "Hello friend!",
                 "The weather is good :)"
             ])
             ->setImageURLs([
                 "https://pbs.twimg.com/media/C6ij4CLUwAAxu9r.jpg",
                 "https://pbs.twimg.com/media/C6jO3UiVoAQYc_8.jpg"
             ])
             ->setLang('en')
             ->withAgeGroup()
             ->withEducationLevel()
             ->withGender()
             ->withIncome()
             ->withOccupation()
             ->withRelationship();
             
$userProfiler = new IndividualUserProfiler($requester);

// Call individual user profile method and get request id
$requestResponse = $userProfiler->predictIndividualUserProfile($request);

$request = (new ResponseRequest())
            ->setRequestID($requestResponse->getRequestId())
            ->setResponseClass(IndividualUserProfile::class);
            
// Call method for check current request status
$commonProcessor = new CommonProcessor($this->requester);

$numAttempts = 10;
$response = null;   /** @var $response IndividualUserProfile */

while($numAttempts-- > 0 && ($response == null || $response->getHttpCode() !== 200)) {
    sleep(self::RESPONSE_WAIT_TIME_SECONDS);
    $response = $commonProcessor->response($request);
}

var_dump($response);

测试

  • 安装PHP Unit
  • 设置环境变量TEST_USERNAMETEST_PASSWORD,分别对应SoMin API的用户名和密码。
  • 在主目录下运行phpunit

API文档

更多信息可以在SoMin开发者网站上找到。