Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add env var to write raw HTTP traffic to stderr #394

Merged
merged 1 commit into from
Apr 27, 2017
Merged
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
24 changes: 23 additions & 1 deletion Twilio/Http/CurlClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
class CurlClient implements Client {
const DEFAULT_TIMEOUT = 60;
protected $curlOptions = array();
protected $debugHttp = false;

public function __construct(array $options = array()) {
$this->curlOptions = $options;
$this->debugHttp = getenv('DEBUG_HTTP_TRAFFIC') === 'true';
}

public function request($method, $url, $params = array(), $data = array(),
Expand All @@ -38,6 +40,20 @@ public function request($method, $url, $params = array(), $data = array(),
? array($parts[1], $parts[2])
: array($parts[0], $parts[1]);

if ($this->debugHttp) {
$u = parse_url($url);
$hdrLine = $method . ' ' . $u['path'];
if (isset($u['query']) && strlen($u['query']) > 0 ) {
$hdrLine = $hdrLine . '?' . $u['query'];
}
error_log($hdrLine);
foreach ($headers as $key => $value) {
error_log("$key: $value");
}
if ($method === 'POST') {
error_log("\n" . $options[CURLOPT_POSTFIELDS] . "\n");
}
}
$statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);

$responseHeaders = array();
Expand All @@ -54,6 +70,13 @@ public function request($method, $url, $params = array(), $data = array(),
fclose($buffer);
}

if ($this->debugHttp) {
error_log("HTTP/1.1 $statusCode");
foreach ($responseHeaders as $key => $value) {
error_log("$key: $value");
}
error_log("\n$body");
}
return new Response($statusCode, $body, $responseHeaders);
} catch (\ErrorException $e) {
if (isset($curl) && is_resource($curl)) {
Expand All @@ -75,7 +98,6 @@ public function options($method, $url, $params = array(), $data = array(),
$timeout = is_null($timeout)
? self::DEFAULT_TIMEOUT
: $timeout;

$options = $this->curlOptions + array(
CURLOPT_URL => $url,
CURLOPT_HEADER => true,
Expand Down