Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add a HTTP stream based HTTP client for environments that lack cURL. #124

Closed
wants to merge 16 commits into from

4 participants

@sjlangley

As cURL is an optional extension not all host have it enabled.

This falls back to a http streams based approach to making HTTP requests is cURL is not detected.

@caseysoftware

Minor typo.. this should be:

verify_peer

Yes, please fix so we can get it all in one shot.

@caseysoftware caseysoftware commented on the diff
Services/Twilio/Resource.php
@@ -87,7 +87,10 @@ public static function decamelize($word)
* @return string
*/
public static function camelize($word) {
- return preg_replace('/(^|_)([a-z])/e', 'strtoupper("\\2")', $word);

As written here, using the anonymous function breaks PHP 5.2 compatibility.

By using create_function - as in my pull request: #123 - we fix the /e issue and keep 5.2 compatibility.

I can revert this then you can merge the other pull request - let me know what's best.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@caseysoftware

Testing complete, this is good.
I'm 100% in favor of merging this pull request given my two notes above.

Services/Twilio.php
((14 lines not shown))
+ CURLOPT_USERAGENT => self::USER_AGENT,
+ CURLOPT_HTTPHEADER => array('Accept-Charset: utf-8'),
+ CURLOPT_CAINFO => dirname(__FILE__) . '/cacert.pem',
+ ))
+ );
+ } else {
+ $_http = new Services_Twilio_HttpStream(
+ "https://api.twilio.com",
+ array(
+ "http_options" => array(
+ "http" => array(
+ "user_agent" => self::USER_AGENT,
+ "header" => "Accept-Charset: utf-8\r\n",
+ ),
+ "ssl" => array(
+ 'veryify_peer' => true,

Looks like there's a typo here.

As noted above. @sjlangley will fix.

done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Services/Twilio/HttpStream.php
((13 lines not shown))
+ private $debug = false;
+ private static $default_options = array(
+ "http" => array(
+ "headers" => "",
+ "timeout" => 60,
+ "follow_location" => true,
+ "ignore_errors" => true,
+ ),
+ "ssl" => array(),
+ );
+ private $options = array();
+
+ public function __construct($uri = '', $kwargs = array()) {
+ $this->uri = $uri;
+ if (isset($kwargs['debug'])) {
+ $this->debug = false;

This line is a little odd... do you mean to write

$this->debug = $kwargs['debug'];

done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@kevinburke kevinburke commented on the diff
Services/Twilio/HttpStream.php
((46 lines not shown))
+
+ foreach($req_headers as $key => $value) {
+ $request_options['http']['header'] .= sprintf("%s: %s\r\n", $key, $value);
+ }
+
+ if (isset($this->auth_header)) {
+ $request_options['http']['header'] .= $this->auth_header;
+ }
+
+ $request_options['http']['method'] = strtoupper($name);
+
+ $ctx = stream_context_create($request_options);
+ $result = file_get_contents($url, false, $ctx);
+
+ if (false === $result) {
+ throw new Services_Twilio_HttpStreamException(

I'm thinking here that we should create a generic HttpException class that both the TinyHttp and the HttpStream class raise in the event of an exception.

I don't see Services_Twilio_TinyHttpException being caught anywhere

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@sjlangley sjlangley - Fix typo in ssl options
- Fix debug flag and print debug output if the flag is on.
814ce39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 1, 2013
  1. Initial version of Twilio with App Engine Support.

    Stuart Langley authored
  2. Use preg_replace_callback rather than the /e option, which is not sup…

    Stuart Langley authored
    …ported on GAE.
  3. Cleanup the URL fetch HTTP class.

    Stuart Langley authored
Commits on Jun 5, 2013
  1. @sjlangley
  2. @sjlangley
  3. @sjlangley
  4. @dataich @sjlangley

    return 'static' object in _call method

    dataich authored sjlangley committed
    sometimes I want to override __call method for some reason. For example I always want to call say method with language option "'language' => 'ja-jp');".
  5. @sjlangley

    Initial version of Twilio with App Engine Support.

    Stuart Langley authored sjlangley committed
  6. @sjlangley

    Use preg_replace_callback rather than the /e option, which is not sup…

    Stuart Langley authored sjlangley committed
    …ported on GAE.
  7. @sjlangley

    Cleanup the URL fetch HTTP class.

    Stuart Langley authored sjlangley committed
  8. @sjlangley
  9. @sjlangley
  10. @sjlangley
  11. @sjlangley
  12. @sjlangley

    Merge remote-tracking branch 'origin/master'

    sjlangley authored
    Conflicts:
    	Services/Twilio.php
Commits on Jun 6, 2013
  1. @sjlangley

    - Fix typo in ssl options

    sjlangley authored
    - Fix debug flag and print debug output if the flag is on.
This page is out of date. Refresh to see the latest.
View
39 Services/Twilio.php
@@ -50,22 +50,33 @@ public function __construct(
$version : end($this->versions);
if (null === $_http) {
- if (!in_array('curl', get_loaded_extensions())) {
- trigger_error("It looks like you do not have curl installed.\n".
- "Curl is required to make HTTP requests using the twilio-php\n" .
- "library. For install instructions, visit the following page:\n" .
- "http://php.net/manual/en/curl.installation.php",
- E_USER_WARNING
+ if (in_array('curl', get_loaded_extensions())) {
+ $_http = new Services_Twilio_TinyHttp(
+ "https://api.twilio.com",
+ array("curlopts" => array(
+ CURLOPT_USERAGENT => self::USER_AGENT,
+ CURLOPT_HTTPHEADER => array('Accept-Charset: utf-8'),
+ CURLOPT_CAINFO => dirname(__FILE__) . '/cacert.pem',
+ ))
+ );
+ } else {
+ $_http = new Services_Twilio_HttpStream(
+ "https://api.twilio.com",
+ array(
+ "http_options" => array(
+ "http" => array(
+ "user_agent" => self::USER_AGENT,
+ "header" => "Accept-Charset: utf-8\r\n",
+ ),
+ "ssl" => array(
+ 'verify_peer' => true,
+ 'cafile' => dirname(__FILE__) . '/cacert.pem',
+ 'verify_depth' => 5,
+ ),
+ ),
+ )
);
}
- $_http = new Services_Twilio_TinyHttp(
- "https://api.twilio.com",
- array("curlopts" => array(
- CURLOPT_USERAGENT => self::USER_AGENT,
- CURLOPT_HTTPHEADER => array('Accept-Charset: utf-8'),
- CURLOPT_CAINFO => dirname(__FILE__) . '/cacert.pem',
- ))
- );
}
$_http->authenticate($sid, $token);
$this->http = $_http;
View
93 Services/Twilio/HttpStream.php
@@ -0,0 +1,93 @@
+<?php
+/**
+ * HTTP Stream version of the TinyHttp Client used to connect to Twilio
+ * services.
+ */
+
+class Services_Twilio_HttpStreamException extends ErrorException {}
+
+class Services_Twilio_HttpStream {
+
+ private $auth_header = null;
+ private $uri = null;
+ private $debug = false;
+ private static $default_options = array(
+ "http" => array(
+ "headers" => "",
+ "timeout" => 60,
+ "follow_location" => true,
+ "ignore_errors" => true,
+ ),
+ "ssl" => array(),
+ );
+ private $options = array();
+
+ public function __construct($uri = '', $kwargs = array()) {
+ $this->uri = $uri;
+ if (isset($kwargs['debug'])) {
+ $this->debug = true;
+ }
+ if (isset($kwargs['http_options'])) {
+ $this->options = $kwargs['http_options'] + self::$default_options;
+ } else {
+ $this->options = self::$default_options;
+ }
+ }
+
+ public function __call($name, $args) {
+ list($res, $req_headers, $req_body) = $args + [0, [], ''];
+
+ $request_options = $this->options;
+ $url = $this->uri . $res;
+
+ if (isset($req_body) && strlen($req_body) > 0) {
+ $request_options['http']['content'] = $req_body;
+ }
+
+ foreach($req_headers as $key => $value) {
+ $request_options['http']['header'] .= sprintf("%s: %s\r\n", $key, $value);
+ }
+
+ if (isset($this->auth_header)) {
+ $request_options['http']['header'] .= $this->auth_header;
+ }
+
+ $request_options['http']['method'] = strtoupper($name);
+
+ if ($this->debug) {
+ error_log(var_export($request_options, true));
+ }
+ $ctx = stream_context_create($request_options);
+ $result = file_get_contents($url, false, $ctx);
+
+ if (false === $result) {
+ throw new Services_Twilio_HttpStreamException(

I'm thinking here that we should create a generic HttpException class that both the TinyHttp and the HttpStream class raise in the event of an exception.

I don't see Services_Twilio_TinyHttpException being caught anywhere

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ "Unable to connect to service");
+ }
+
+ $status_header = array_shift($http_response_header);
+ if (1 !== preg_match('#HTTP/\d+\.\d+ (\d+)#', $status_header, $matches)) {
+ throw new Services_Twilio_HttpStreamException(
+ "Unable to detect the status code in the HTTP result.");
+ }
+
+ $status_code = intval($matches[1]);
+ $response_headers = array();
+
+ foreach($http_response_header as $header) {
+ list($key, $val) = explode(":", $header);
+ $response_headers[trim($key)] = trim($val);
+ }
+
+ return array($status_code, $response_headers, $result);
+ }
+
+ public function authenticate($user, $pass) {
+ if (isset($user) && isset($pass)) {
+ $this->auth_header = sprintf("Authorization: Basic %s",
+ base64_encode(sprintf("%s:%s", $user, $pass)));
+ } else {
+ $this->auth_header = null;
+ }
+ }
+}
View
5 Services/Twilio/Resource.php
@@ -87,7 +87,10 @@ public static function decamelize($word)
* @return string
*/
public static function camelize($word) {
- return preg_replace('/(^|_)([a-z])/e', 'strtoupper("\\2")', $word);

As written here, using the anonymous function breaks PHP 5.2 compatibility.

By using create_function - as in my pull request: #123 - we fix the /e issue and keep 5.2 compatibility.

I can revert this then you can merge the other pull request - let me know what's best.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ $callback = function($matches) {
+ return strtoupper($matches[2]);
+ };
+ return preg_replace_callback('/(^|_)([a-z])/', $callback, $word);
}
/**
Something went wrong with that request. Please try again.