2018-01-28 12:17:19 -05:00
|
|
|
<?php
|
2021-09-13 14:51:12 -04:00
|
|
|
|
2018-01-28 12:17:19 -05:00
|
|
|
/**
|
|
|
|
* The most popular PHP library for use with the Twitter OAuth REST API.
|
|
|
|
*
|
|
|
|
* @license MIT
|
|
|
|
*/
|
2021-09-13 14:51:12 -04:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-01-28 12:17:19 -05:00
|
|
|
namespace Abraham\TwitterOAuth;
|
|
|
|
|
|
|
|
use Abraham\TwitterOAuth\Util\JsonDecoder;
|
2021-09-13 14:51:12 -04:00
|
|
|
use Composer\CaBundle\CaBundle;
|
2018-01-28 12:17:19 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* TwitterOAuth class for interacting with the Twitter API.
|
|
|
|
*
|
|
|
|
* @author Abraham Williams <abraham@abrah.am>
|
|
|
|
*/
|
|
|
|
class TwitterOAuth extends Config
|
|
|
|
{
|
2021-09-13 14:51:12 -04:00
|
|
|
private const API_VERSION = '1.1';
|
|
|
|
private const API_HOST = 'https://api.twitter.com';
|
|
|
|
private const UPLOAD_HOST = 'https://upload.twitter.com';
|
2018-01-28 12:17:19 -05:00
|
|
|
|
|
|
|
/** @var Response details about the result of the last request */
|
|
|
|
private $response;
|
|
|
|
/** @var string|null Application bearer token */
|
|
|
|
private $bearer;
|
|
|
|
/** @var Consumer Twitter application details */
|
|
|
|
private $consumer;
|
|
|
|
/** @var Token|null User access token details */
|
|
|
|
private $token;
|
|
|
|
/** @var HmacSha1 OAuth 1 signature type used by Twitter */
|
|
|
|
private $signatureMethod;
|
2021-09-13 14:51:12 -04:00
|
|
|
/** @var int Number of attempts we made for the request */
|
|
|
|
private $attempts = 0;
|
2018-01-28 12:17:19 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param string $consumerKey The Application Consumer Key
|
|
|
|
* @param string $consumerSecret The Application Consumer Secret
|
|
|
|
* @param string|null $oauthToken The Client Token (optional)
|
|
|
|
* @param string|null $oauthTokenSecret The Client Token Secret (optional)
|
|
|
|
*/
|
2021-09-13 14:51:12 -04:00
|
|
|
public function __construct(
|
|
|
|
string $consumerKey,
|
|
|
|
string $consumerSecret,
|
|
|
|
?string $oauthToken = null,
|
|
|
|
?string $oauthTokenSecret = null
|
|
|
|
) {
|
2018-01-28 12:17:19 -05:00
|
|
|
$this->resetLastResponse();
|
|
|
|
$this->signatureMethod = new HmacSha1();
|
|
|
|
$this->consumer = new Consumer($consumerKey, $consumerSecret);
|
|
|
|
if (!empty($oauthToken) && !empty($oauthTokenSecret)) {
|
2021-09-13 14:51:12 -04:00
|
|
|
$this->setOauthToken($oauthToken, $oauthTokenSecret);
|
2018-01-28 12:17:19 -05:00
|
|
|
}
|
|
|
|
if (empty($oauthToken) && !empty($oauthTokenSecret)) {
|
2021-09-13 14:51:12 -04:00
|
|
|
$this->setBearer($oauthTokenSecret);
|
2018-01-28 12:17:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $oauthToken
|
|
|
|
* @param string $oauthTokenSecret
|
|
|
|
*/
|
2021-09-13 14:51:12 -04:00
|
|
|
public function setOauthToken(
|
|
|
|
string $oauthToken,
|
|
|
|
string $oauthTokenSecret
|
|
|
|
): void {
|
2018-01-28 12:17:19 -05:00
|
|
|
$this->token = new Token($oauthToken, $oauthTokenSecret);
|
2021-09-13 14:51:12 -04:00
|
|
|
$this->bearer = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $oauthTokenSecret
|
|
|
|
*/
|
|
|
|
public function setBearer(string $oauthTokenSecret): void
|
|
|
|
{
|
|
|
|
$this->bearer = $oauthTokenSecret;
|
|
|
|
$this->token = null;
|
2018-01-28 12:17:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string|null
|
|
|
|
*/
|
2021-09-13 14:51:12 -04:00
|
|
|
public function getLastApiPath(): ?string
|
2018-01-28 12:17:19 -05:00
|
|
|
{
|
|
|
|
return $this->response->getApiPath();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
2021-09-13 14:51:12 -04:00
|
|
|
public function getLastHttpCode(): int
|
2018-01-28 12:17:19 -05:00
|
|
|
{
|
|
|
|
return $this->response->getHttpCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2021-09-13 14:51:12 -04:00
|
|
|
public function getLastXHeaders(): array
|
2018-01-28 12:17:19 -05:00
|
|
|
{
|
|
|
|
return $this->response->getXHeaders();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array|object|null
|
|
|
|
*/
|
|
|
|
public function getLastBody()
|
|
|
|
{
|
|
|
|
return $this->response->getBody();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resets the last response cache.
|
|
|
|
*/
|
2021-09-13 14:51:12 -04:00
|
|
|
public function resetLastResponse(): void
|
2018-01-28 12:17:19 -05:00
|
|
|
{
|
|
|
|
$this->response = new Response();
|
|
|
|
}
|
|
|
|
|
2021-09-13 14:51:12 -04:00
|
|
|
/**
|
|
|
|
* Resets the attempts number.
|
|
|
|
*/
|
|
|
|
private function resetAttemptsNumber(): void
|
|
|
|
{
|
|
|
|
$this->attempts = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delays the retries when they're activated.
|
|
|
|
*/
|
|
|
|
private function sleepIfNeeded(): void
|
|
|
|
{
|
|
|
|
if ($this->maxRetries && $this->attempts) {
|
|
|
|
sleep($this->retriesDelay);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-28 12:17:19 -05:00
|
|
|
/**
|
|
|
|
* Make URLs for user browser navigation.
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @param array $parameters
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2021-09-13 14:51:12 -04:00
|
|
|
public function url(string $path, array $parameters): string
|
2018-01-28 12:17:19 -05:00
|
|
|
{
|
|
|
|
$this->resetLastResponse();
|
|
|
|
$this->response->setApiPath($path);
|
|
|
|
$query = http_build_query($parameters);
|
|
|
|
return sprintf('%s/%s?%s', self::API_HOST, $path, $query);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make /oauth/* requests to the API.
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @param array $parameters
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
* @throws TwitterOAuthException
|
|
|
|
*/
|
2021-09-13 14:51:12 -04:00
|
|
|
public function oauth(string $path, array $parameters = []): array
|
2018-01-28 12:17:19 -05:00
|
|
|
{
|
|
|
|
$response = [];
|
|
|
|
$this->resetLastResponse();
|
|
|
|
$this->response->setApiPath($path);
|
|
|
|
$url = sprintf('%s/%s', self::API_HOST, $path);
|
|
|
|
$result = $this->oAuthRequest($url, 'POST', $parameters);
|
|
|
|
|
|
|
|
if ($this->getLastHttpCode() != 200) {
|
|
|
|
throw new TwitterOAuthException($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
parse_str($result, $response);
|
|
|
|
$this->response->setBody($response);
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make /oauth2/* requests to the API.
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @param array $parameters
|
|
|
|
*
|
|
|
|
* @return array|object
|
|
|
|
*/
|
2021-09-13 14:51:12 -04:00
|
|
|
public function oauth2(string $path, array $parameters = [])
|
2018-01-28 12:17:19 -05:00
|
|
|
{
|
|
|
|
$method = 'POST';
|
|
|
|
$this->resetLastResponse();
|
|
|
|
$this->response->setApiPath($path);
|
|
|
|
$url = sprintf('%s/%s', self::API_HOST, $path);
|
2021-09-13 14:51:12 -04:00
|
|
|
$request = Request::fromConsumerAndToken(
|
|
|
|
$this->consumer,
|
|
|
|
$this->token,
|
|
|
|
$method,
|
|
|
|
$url,
|
|
|
|
$parameters
|
|
|
|
);
|
|
|
|
$authorization =
|
|
|
|
'Authorization: Basic ' .
|
|
|
|
$this->encodeAppAuthorization($this->consumer);
|
|
|
|
$result = $this->request(
|
|
|
|
$request->getNormalizedHttpUrl(),
|
|
|
|
$method,
|
|
|
|
$authorization,
|
|
|
|
$parameters
|
|
|
|
);
|
2018-01-28 12:17:19 -05:00
|
|
|
$response = JsonDecoder::decode($result, $this->decodeJsonAsArray);
|
|
|
|
$this->response->setBody($response);
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make GET requests to the API.
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @param array $parameters
|
|
|
|
*
|
|
|
|
* @return array|object
|
|
|
|
*/
|
2021-09-13 14:51:12 -04:00
|
|
|
public function get(string $path, array $parameters = [])
|
2018-01-28 12:17:19 -05:00
|
|
|
{
|
2021-09-13 14:51:12 -04:00
|
|
|
return $this->http('GET', self::API_HOST, $path, $parameters, false);
|
2018-01-28 12:17:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make POST requests to the API.
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @param array $parameters
|
2021-09-13 14:51:12 -04:00
|
|
|
* @param bool $json
|
2018-01-28 12:17:19 -05:00
|
|
|
*
|
|
|
|
* @return array|object
|
|
|
|
*/
|
2021-09-13 14:51:12 -04:00
|
|
|
public function post(
|
|
|
|
string $path,
|
|
|
|
array $parameters = [],
|
|
|
|
bool $json = false
|
|
|
|
) {
|
|
|
|
return $this->http('POST', self::API_HOST, $path, $parameters, $json);
|
2018-01-28 12:17:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make DELETE requests to the API.
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @param array $parameters
|
|
|
|
*
|
|
|
|
* @return array|object
|
|
|
|
*/
|
2021-09-13 14:51:12 -04:00
|
|
|
public function delete(string $path, array $parameters = [])
|
2018-01-28 12:17:19 -05:00
|
|
|
{
|
2021-09-13 14:51:12 -04:00
|
|
|
return $this->http('DELETE', self::API_HOST, $path, $parameters, false);
|
2018-01-28 12:17:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make PUT requests to the API.
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @param array $parameters
|
|
|
|
*
|
|
|
|
* @return array|object
|
|
|
|
*/
|
2021-09-13 14:51:12 -04:00
|
|
|
public function put(string $path, array $parameters = [])
|
2018-01-28 12:17:19 -05:00
|
|
|
{
|
2021-09-13 14:51:12 -04:00
|
|
|
return $this->http('PUT', self::API_HOST, $path, $parameters, false);
|
2018-01-28 12:17:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Upload media to upload.twitter.com.
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @param array $parameters
|
|
|
|
* @param boolean $chunked
|
|
|
|
*
|
|
|
|
* @return array|object
|
|
|
|
*/
|
2021-09-13 14:51:12 -04:00
|
|
|
public function upload(
|
|
|
|
string $path,
|
|
|
|
array $parameters = [],
|
|
|
|
bool $chunked = false
|
|
|
|
) {
|
2018-01-28 12:17:19 -05:00
|
|
|
if ($chunked) {
|
|
|
|
return $this->uploadMediaChunked($path, $parameters);
|
|
|
|
} else {
|
|
|
|
return $this->uploadMediaNotChunked($path, $parameters);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-13 14:51:12 -04:00
|
|
|
/**
|
|
|
|
* Progression of media upload
|
|
|
|
*
|
|
|
|
* @param string $media_id
|
|
|
|
*
|
|
|
|
* @return array|object
|
|
|
|
*/
|
|
|
|
public function mediaStatus(string $media_id)
|
|
|
|
{
|
|
|
|
return $this->http(
|
|
|
|
'GET',
|
|
|
|
self::UPLOAD_HOST,
|
|
|
|
'media/upload',
|
|
|
|
[
|
|
|
|
'command' => 'STATUS',
|
|
|
|
'media_id' => $media_id,
|
|
|
|
],
|
|
|
|
false
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-01-28 12:17:19 -05:00
|
|
|
/**
|
|
|
|
* Private method to upload media (not chunked) to upload.twitter.com.
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @param array $parameters
|
|
|
|
*
|
|
|
|
* @return array|object
|
|
|
|
*/
|
2021-09-13 14:51:12 -04:00
|
|
|
private function uploadMediaNotChunked(string $path, array $parameters)
|
2018-01-28 12:17:19 -05:00
|
|
|
{
|
2021-09-13 14:51:12 -04:00
|
|
|
if (
|
|
|
|
!is_readable($parameters['media']) ||
|
|
|
|
($file = file_get_contents($parameters['media'])) === false
|
|
|
|
) {
|
|
|
|
throw new \InvalidArgumentException(
|
|
|
|
'You must supply a readable file'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$parameters['media'] = base64_encode($file);
|
|
|
|
return $this->http(
|
|
|
|
'POST',
|
|
|
|
self::UPLOAD_HOST,
|
|
|
|
$path,
|
|
|
|
$parameters,
|
|
|
|
false
|
|
|
|
);
|
2018-01-28 12:17:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Private method to upload media (chunked) to upload.twitter.com.
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @param array $parameters
|
|
|
|
*
|
|
|
|
* @return array|object
|
|
|
|
*/
|
2021-09-13 14:51:12 -04:00
|
|
|
private function uploadMediaChunked(string $path, array $parameters)
|
2018-01-28 12:17:19 -05:00
|
|
|
{
|
2021-09-13 14:51:12 -04:00
|
|
|
$init = $this->http(
|
|
|
|
'POST',
|
|
|
|
self::UPLOAD_HOST,
|
|
|
|
$path,
|
|
|
|
$this->mediaInitParameters($parameters),
|
|
|
|
false
|
|
|
|
);
|
2018-01-28 12:17:19 -05:00
|
|
|
// Append
|
|
|
|
$segmentIndex = 0;
|
|
|
|
$media = fopen($parameters['media'], 'rb');
|
2021-09-13 14:51:12 -04:00
|
|
|
while (!feof($media)) {
|
|
|
|
$this->http(
|
|
|
|
'POST',
|
|
|
|
self::UPLOAD_HOST,
|
|
|
|
'media/upload',
|
|
|
|
[
|
|
|
|
'command' => 'APPEND',
|
|
|
|
'media_id' => $init->media_id_string,
|
|
|
|
'segment_index' => $segmentIndex++,
|
|
|
|
'media_data' => base64_encode(
|
|
|
|
fread($media, $this->chunkSize)
|
|
|
|
),
|
|
|
|
],
|
|
|
|
false
|
|
|
|
);
|
2018-01-28 12:17:19 -05:00
|
|
|
}
|
|
|
|
fclose($media);
|
|
|
|
// Finalize
|
2021-09-13 14:51:12 -04:00
|
|
|
$finalize = $this->http(
|
|
|
|
'POST',
|
|
|
|
self::UPLOAD_HOST,
|
|
|
|
'media/upload',
|
|
|
|
[
|
|
|
|
'command' => 'FINALIZE',
|
|
|
|
'media_id' => $init->media_id_string,
|
|
|
|
],
|
|
|
|
false
|
|
|
|
);
|
2018-01-28 12:17:19 -05:00
|
|
|
return $finalize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Private method to get params for upload media chunked init.
|
|
|
|
* Twitter docs: https://dev.twitter.com/rest/reference/post/media/upload-init.html
|
|
|
|
*
|
|
|
|
* @param array $parameters
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2021-09-13 14:51:12 -04:00
|
|
|
private function mediaInitParameters(array $parameters): array
|
2018-01-28 12:17:19 -05:00
|
|
|
{
|
2021-09-13 14:51:12 -04:00
|
|
|
$allowed_keys = [
|
|
|
|
'media_type',
|
|
|
|
'additional_owners',
|
|
|
|
'media_category',
|
|
|
|
'shared',
|
|
|
|
];
|
|
|
|
$base = [
|
2018-01-28 12:17:19 -05:00
|
|
|
'command' => 'INIT',
|
2021-09-13 14:51:12 -04:00
|
|
|
'total_bytes' => filesize($parameters['media']),
|
2018-01-28 12:17:19 -05:00
|
|
|
];
|
2021-09-13 14:51:12 -04:00
|
|
|
$allowed_parameters = array_intersect_key(
|
|
|
|
$parameters,
|
|
|
|
array_flip($allowed_keys)
|
|
|
|
);
|
|
|
|
return array_merge($base, $allowed_parameters);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cleanup any parameters that are known not to work.
|
|
|
|
*
|
|
|
|
* @param array $parameters
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function cleanUpParameters(array $parameters)
|
|
|
|
{
|
|
|
|
foreach ($parameters as $key => $value) {
|
|
|
|
// PHP coerces `true` to `"1"` which some Twitter APIs don't like.
|
|
|
|
if (is_bool($value)) {
|
|
|
|
$parameters[$key] = var_export($value, true);
|
|
|
|
}
|
2018-01-28 12:17:19 -05:00
|
|
|
}
|
2021-09-13 14:51:12 -04:00
|
|
|
return $parameters;
|
2018-01-28 12:17:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $method
|
|
|
|
* @param string $host
|
|
|
|
* @param string $path
|
|
|
|
* @param array $parameters
|
2021-09-13 14:51:12 -04:00
|
|
|
* @param bool $json
|
2018-01-28 12:17:19 -05:00
|
|
|
*
|
|
|
|
* @return array|object
|
|
|
|
*/
|
2021-09-13 14:51:12 -04:00
|
|
|
private function http(
|
|
|
|
string $method,
|
|
|
|
string $host,
|
|
|
|
string $path,
|
|
|
|
array $parameters,
|
|
|
|
bool $json
|
|
|
|
) {
|
2018-01-28 12:17:19 -05:00
|
|
|
$this->resetLastResponse();
|
2021-09-13 14:51:12 -04:00
|
|
|
$this->resetAttemptsNumber();
|
2018-01-28 12:17:19 -05:00
|
|
|
$url = sprintf('%s/%s/%s.json', $host, self::API_VERSION, $path);
|
|
|
|
$this->response->setApiPath($path);
|
2021-09-13 14:51:12 -04:00
|
|
|
if (!$json) {
|
|
|
|
$parameters = $this->cleanUpParameters($parameters);
|
|
|
|
}
|
|
|
|
return $this->makeRequests($url, $method, $parameters, $json);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Make requests and retry them (if enabled) in case of Twitter's problems.
|
|
|
|
*
|
|
|
|
* @param string $method
|
|
|
|
* @param string $url
|
|
|
|
* @param string $method
|
|
|
|
* @param array $parameters
|
|
|
|
* @param bool $json
|
|
|
|
*
|
|
|
|
* @return array|object
|
|
|
|
*/
|
|
|
|
private function makeRequests(
|
|
|
|
string $url,
|
|
|
|
string $method,
|
|
|
|
array $parameters,
|
|
|
|
bool $json
|
|
|
|
) {
|
|
|
|
do {
|
|
|
|
$this->sleepIfNeeded();
|
|
|
|
$result = $this->oAuthRequest($url, $method, $parameters, $json);
|
|
|
|
$response = JsonDecoder::decode($result, $this->decodeJsonAsArray);
|
|
|
|
$this->response->setBody($response);
|
|
|
|
$this->attempts++;
|
|
|
|
// Retry up to our $maxRetries number if we get errors greater than 500 (over capacity etc)
|
|
|
|
} while ($this->requestsAvailable());
|
|
|
|
|
2018-01-28 12:17:19 -05:00
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2021-09-13 14:51:12 -04:00
|
|
|
/**
|
|
|
|
* Checks if we have to retry request if API is down.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function requestsAvailable(): bool
|
|
|
|
{
|
|
|
|
return $this->maxRetries &&
|
|
|
|
$this->attempts <= $this->maxRetries &&
|
|
|
|
$this->getLastHttpCode() >= 500;
|
|
|
|
}
|
|
|
|
|
2018-01-28 12:17:19 -05:00
|
|
|
/**
|
|
|
|
* Format and sign an OAuth / API request
|
|
|
|
*
|
|
|
|
* @param string $url
|
|
|
|
* @param string $method
|
|
|
|
* @param array $parameters
|
2021-09-13 14:51:12 -04:00
|
|
|
* @param bool $json
|
2018-01-28 12:17:19 -05:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
* @throws TwitterOAuthException
|
|
|
|
*/
|
2021-09-13 14:51:12 -04:00
|
|
|
private function oAuthRequest(
|
|
|
|
string $url,
|
|
|
|
string $method,
|
|
|
|
array $parameters,
|
|
|
|
bool $json = false
|
|
|
|
) {
|
|
|
|
$request = Request::fromConsumerAndToken(
|
|
|
|
$this->consumer,
|
|
|
|
$this->token,
|
|
|
|
$method,
|
|
|
|
$url,
|
|
|
|
$parameters,
|
|
|
|
$json
|
|
|
|
);
|
2018-01-28 12:17:19 -05:00
|
|
|
if (array_key_exists('oauth_callback', $parameters)) {
|
|
|
|
// Twitter doesn't like oauth_callback as a parameter.
|
|
|
|
unset($parameters['oauth_callback']);
|
|
|
|
}
|
|
|
|
if ($this->bearer === null) {
|
2021-09-13 14:51:12 -04:00
|
|
|
$request->signRequest(
|
|
|
|
$this->signatureMethod,
|
|
|
|
$this->consumer,
|
|
|
|
$this->token
|
|
|
|
);
|
2018-01-28 12:17:19 -05:00
|
|
|
$authorization = $request->toHeader();
|
|
|
|
if (array_key_exists('oauth_verifier', $parameters)) {
|
|
|
|
// Twitter doesn't always work with oauth in the body and in the header
|
|
|
|
// and it's already included in the $authorization header
|
|
|
|
unset($parameters['oauth_verifier']);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$authorization = 'Authorization: Bearer ' . $this->bearer;
|
|
|
|
}
|
2021-09-13 14:51:12 -04:00
|
|
|
return $this->request(
|
|
|
|
$request->getNormalizedHttpUrl(),
|
|
|
|
$method,
|
|
|
|
$authorization,
|
|
|
|
$parameters,
|
|
|
|
$json
|
|
|
|
);
|
2018-01-28 12:17:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set Curl options.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2021-09-13 14:51:12 -04:00
|
|
|
private function curlOptions(): array
|
2018-01-28 12:17:19 -05:00
|
|
|
{
|
2021-09-13 14:51:12 -04:00
|
|
|
$bundlePath = CaBundle::getSystemCaRootBundlePath();
|
2018-01-28 12:17:19 -05:00
|
|
|
$options = [
|
|
|
|
// CURLOPT_VERBOSE => true,
|
|
|
|
CURLOPT_CONNECTTIMEOUT => $this->connectionTimeout,
|
|
|
|
CURLOPT_HEADER => true,
|
|
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
|
|
CURLOPT_SSL_VERIFYHOST => 2,
|
|
|
|
CURLOPT_SSL_VERIFYPEER => true,
|
|
|
|
CURLOPT_TIMEOUT => $this->timeout,
|
|
|
|
CURLOPT_USERAGENT => $this->userAgent,
|
2021-09-13 14:51:12 -04:00
|
|
|
$this->curlCaOpt($bundlePath) => $bundlePath,
|
2018-01-28 12:17:19 -05:00
|
|
|
];
|
|
|
|
|
2021-09-13 14:51:12 -04:00
|
|
|
if ($this->gzipEncoding) {
|
2018-01-28 12:17:19 -05:00
|
|
|
$options[CURLOPT_ENCODING] = 'gzip';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($this->proxy)) {
|
|
|
|
$options[CURLOPT_PROXY] = $this->proxy['CURLOPT_PROXY'];
|
2021-09-13 14:51:12 -04:00
|
|
|
$options[CURLOPT_PROXYUSERPWD] =
|
|
|
|
$this->proxy['CURLOPT_PROXYUSERPWD'];
|
2018-01-28 12:17:19 -05:00
|
|
|
$options[CURLOPT_PROXYPORT] = $this->proxy['CURLOPT_PROXYPORT'];
|
|
|
|
$options[CURLOPT_PROXYAUTH] = CURLAUTH_BASIC;
|
|
|
|
$options[CURLOPT_PROXYTYPE] = CURLPROXY_HTTP;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $options;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make an HTTP request
|
|
|
|
*
|
|
|
|
* @param string $url
|
|
|
|
* @param string $method
|
|
|
|
* @param string $authorization
|
|
|
|
* @param array $postfields
|
2021-09-13 14:51:12 -04:00
|
|
|
* @param bool $json
|
2018-01-28 12:17:19 -05:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
* @throws TwitterOAuthException
|
|
|
|
*/
|
2021-09-13 14:51:12 -04:00
|
|
|
private function request(
|
|
|
|
string $url,
|
|
|
|
string $method,
|
|
|
|
string $authorization,
|
|
|
|
array $postfields,
|
|
|
|
bool $json = false
|
|
|
|
): string {
|
|
|
|
$options = $this->curlOptions();
|
2018-01-28 12:17:19 -05:00
|
|
|
$options[CURLOPT_URL] = $url;
|
2021-09-13 14:51:12 -04:00
|
|
|
$options[CURLOPT_HTTPHEADER] = [
|
|
|
|
'Accept: application/json',
|
|
|
|
$authorization,
|
|
|
|
'Expect:',
|
|
|
|
];
|
2018-01-28 12:17:19 -05:00
|
|
|
|
|
|
|
switch ($method) {
|
|
|
|
case 'GET':
|
|
|
|
break;
|
|
|
|
case 'POST':
|
|
|
|
$options[CURLOPT_POST] = true;
|
2021-09-13 14:51:12 -04:00
|
|
|
if ($json) {
|
|
|
|
$options[CURLOPT_HTTPHEADER][] =
|
|
|
|
'Content-type: application/json';
|
|
|
|
$options[CURLOPT_POSTFIELDS] = json_encode($postfields);
|
|
|
|
} else {
|
|
|
|
$options[CURLOPT_POSTFIELDS] = Util::buildHttpQuery(
|
|
|
|
$postfields
|
|
|
|
);
|
|
|
|
}
|
2018-01-28 12:17:19 -05:00
|
|
|
break;
|
|
|
|
case 'DELETE':
|
|
|
|
$options[CURLOPT_CUSTOMREQUEST] = 'DELETE';
|
|
|
|
break;
|
|
|
|
case 'PUT':
|
|
|
|
$options[CURLOPT_CUSTOMREQUEST] = 'PUT';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-09-13 14:51:12 -04:00
|
|
|
if (
|
|
|
|
in_array($method, ['GET', 'PUT', 'DELETE']) &&
|
|
|
|
!empty($postfields)
|
|
|
|
) {
|
2018-01-28 12:17:19 -05:00
|
|
|
$options[CURLOPT_URL] .= '?' . Util::buildHttpQuery($postfields);
|
|
|
|
}
|
|
|
|
|
|
|
|
$curlHandle = curl_init();
|
|
|
|
curl_setopt_array($curlHandle, $options);
|
|
|
|
$response = curl_exec($curlHandle);
|
|
|
|
|
|
|
|
// Throw exceptions on cURL errors.
|
|
|
|
if (curl_errno($curlHandle) > 0) {
|
2021-09-13 14:51:12 -04:00
|
|
|
$error = curl_error($curlHandle);
|
|
|
|
$errorNo = curl_errno($curlHandle);
|
|
|
|
curl_close($curlHandle);
|
|
|
|
throw new TwitterOAuthException($error, $errorNo);
|
2018-01-28 12:17:19 -05:00
|
|
|
}
|
|
|
|
|
2021-09-13 14:51:12 -04:00
|
|
|
$this->response->setHttpCode(
|
|
|
|
curl_getinfo($curlHandle, CURLINFO_HTTP_CODE)
|
|
|
|
);
|
2018-01-28 12:17:19 -05:00
|
|
|
$parts = explode("\r\n\r\n", $response);
|
|
|
|
$responseBody = array_pop($parts);
|
|
|
|
$responseHeader = array_pop($parts);
|
|
|
|
$this->response->setHeaders($this->parseHeaders($responseHeader));
|
|
|
|
|
|
|
|
curl_close($curlHandle);
|
|
|
|
|
|
|
|
return $responseBody;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the header info to store.
|
|
|
|
*
|
|
|
|
* @param string $header
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2021-09-13 14:51:12 -04:00
|
|
|
private function parseHeaders(string $header): array
|
2018-01-28 12:17:19 -05:00
|
|
|
{
|
|
|
|
$headers = [];
|
|
|
|
foreach (explode("\r\n", $header) as $line) {
|
|
|
|
if (strpos($line, ':') !== false) {
|
2021-09-13 14:51:12 -04:00
|
|
|
[$key, $value] = explode(': ', $line);
|
2018-01-28 12:17:19 -05:00
|
|
|
$key = str_replace('-', '_', strtolower($key));
|
|
|
|
$headers[$key] = trim($value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $headers;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Encode application authorization header with base64.
|
|
|
|
*
|
|
|
|
* @param Consumer $consumer
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2021-09-13 14:51:12 -04:00
|
|
|
private function encodeAppAuthorization(Consumer $consumer): string
|
2018-01-28 12:17:19 -05:00
|
|
|
{
|
|
|
|
$key = rawurlencode($consumer->key);
|
|
|
|
$secret = rawurlencode($consumer->secret);
|
|
|
|
return base64_encode($key . ':' . $secret);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-09-13 14:51:12 -04:00
|
|
|
* Get Curl CA option based on whether the given path is a directory or file.
|
2018-01-28 12:17:19 -05:00
|
|
|
*
|
2021-09-13 14:51:12 -04:00
|
|
|
* @param string $path
|
|
|
|
* @return int
|
2018-01-28 12:17:19 -05:00
|
|
|
*/
|
2021-09-13 14:51:12 -04:00
|
|
|
private function curlCaOpt(string $path): int
|
2018-01-28 12:17:19 -05:00
|
|
|
{
|
2021-09-13 14:51:12 -04:00
|
|
|
return is_dir($path) ? CURLOPT_CAPATH : CURLOPT_CAINFO;
|
2018-01-28 12:17:19 -05:00
|
|
|
}
|
|
|
|
}
|