Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion src/Twitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ class Twitter
CURLOPT_USERAGENT => 'Twitter for PHP',
];

/** @var array */
public $headers;

/** @var string */
public $body;

/** @var OAuth\Consumer */
private $consumer;

Expand Down Expand Up @@ -312,9 +318,11 @@ public function request(string $resource, string $method, array $data = [], arra
$request->sign_request(new OAuth\SignatureMethod_HMAC_SHA1, $this->consumer, $this->token);
$headers[] = $request->to_header();

$this->headers = []; // Header array cleanup

$options = [
CURLOPT_URL => $resource,
CURLOPT_HEADER => false,
CURLOPT_HEADERFUNCTION => [$this, '_setHeader'],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $headers,
] + $this->httpOptions;
Expand All @@ -338,6 +346,8 @@ public function request(string $resource, string $method, array $data = [], arra
throw new Exception('Server error: ' . curl_error($curl));
}

$this->body = $result; // RAW body

if (strpos(curl_getinfo($curl, CURLINFO_CONTENT_TYPE), 'application/json') !== false) {
$payload = @json_decode($result, false, 128, JSON_BIGINT_AS_STRING); // intentionally @
if ($payload === false) {
Expand Down Expand Up @@ -432,6 +442,30 @@ public static function clickable(stdClass $status): string
}
return $s;
}


/**
* Saves headers to the instance
*/
private function _setHeader($ch, $header)
{
$this->headers[] = trim($header);
return strlen($header);
}

/**
* Get headers in an indexed array
*/
public function getHeaderArray()
{
$headers_array = [];
foreach ($this->headers as $h) {
if(preg_match_all('/^([A-Za-z-]+)\: ([ -~]+)/', $h, $matches, PREG_SET_ORDER, 0)){
$headers_array[$matches[0][1]] = $matches[0][2];
}
}
return $headers_array;
}
}


Expand Down