title: Installing cURL in a Twitter / CodeIgniter project
body: <p>I'm new to Twitter API version 1.1 and was following <a href="https://stackoverflow.com/questions/12916539/simplest-php-example-retrieving-user-timeline-with-twitter-api-version-1-1">Jimbo's excellent answer to this question</a> when I encountered this error when running <code>http://localhost/twitter_test.php</code> :</p>
  
  <blockquote>
    <p>You need to install cURL, see: <a href="http://curl.haxx.se/docs/install.html" rel="nofollow noreferrer">http://curl.haxx.se/docs/install.html</a></p>
  </blockquote>
  
  <p>Although I already have cURL installed on my XP machine, how do I "install cURL" in this test codeigniter project?</p>
  
  <p><strong>twitter_test.php</strong>
      
  
  <pre><code>// Set access tokens here
  $settings = array(
      'oauth_access_token' =&gt; "My Access Token",
      'oauth_access_token_secret' =&gt; "My Access Secret",
      'consumer_key' =&gt; "My Consumer Key",
      'consumer_secret' =&gt; "My Consumer Secret"   
      );  
  
  $url = 'https://api.twitter.com/1.1/followers/ids.json';
  $getfield = '?username=somename';
  $requestMethod = 'GET';
  $twitter = new TwitterAPIExchange($settings);
  echo $twitter-&gt;setGetfield($getfield)
               -&gt;buildOauth($url, $requestMethod)
               -&gt;performRequest();
  ?&gt;
  </code></pre>
  
  <p><strong>TwitterAPIExchange.php</strong> (source: <a href="https://github.com/J7mbo/twitter-api-php" rel="nofollow noreferrer">j7mbo / twitter-api-php</a>)</p>
  
  <pre><code>&lt;?php
  
  /**
   * Twitter-API-PHP : Simple PHP wrapper for the v1.1 API
   * 
   * PHP version 5.3.10
   * 
   * @category Awesomeness
   * @package  Twitter-API-PHP
   * @author   James Mallison &lt;me@j7mbo.co.uk&gt;
   * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
   * @link     http://github.com/j7mbo/twitter-api-php
   */
  class TwitterAPIExchange 
  {
      private $oauth_access_token;
      private $oauth_access_token_secret;
      private $consumer_key;
      private $consumer_secret;
      private $postfields;
      private $getfield;
      protected $oauth;
      public $url;
  
      /**
       * Create the API access object. Requires an array of settings::
       * oauth access token, oauth access token secret, consumer key, consumer secret
       * These are all available by creating your own application on dev.twitter.com
       * Requires the cURL library
       * 
       * @param array $settings
       */
      public function __construct(array $settings)
      {
          if (!in_array('curl', get_loaded_extensions())) 
          {
              exit('You need to install cURL, see: http://curl.haxx.se/docs/install.html');
          }
  
          if (!isset($settings['oauth_access_token'])
              || !isset($settings['oauth_access_token_secret'])
              || !isset($settings['consumer_key'])
              || !isset($settings['consumer_secret']))
          {
              exit('Make sure you are passing in the correct parameters');
          }
  
          $this-&gt;oauth_access_token = $settings['oauth_access_token'];
          $this-&gt;oauth_access_token_secret = $settings['oauth_access_token_secret'];
          $this-&gt;consumer_key = $settings['consumer_key'];
          $this-&gt;consumer_secret = $settings['consumer_secret'];
      }
  
      /**
       * Set postfields array, example: array('screen_name' =&gt; 'J7mbo')
       * 
       * @param array $array Array of parameters to send to API
       * @return \TwitterAPIExchange Instance of self for method chaining
       */
      public function setPostfields(array $array)
      {
          if (!is_null($this-&gt;getGetfield())) 
          { 
              exit('You can only choose get OR post fields.'); 
          }
          $this-&gt;postfields = $array;
          return $this;
      }
  
      /**
       * Set getfield string, example: '?screen_name=J7mbo'
       * 
       * @param string $string Get key and value pairs as string
       * @return \TwitterAPIExchange Instance of self for method chaining
       */
      public function setGetfield($string)
      {
          if (!is_null($this-&gt;getPostfields())) 
          { 
              exit('You can only choose get OR post fields.'); 
          }
  
          $this-&gt;getfield = $string;
          return $this;
      }
  
      /**
       * Get getfield string (simple getter)
       * 
       * @return string $this-&gt;getfields
       */
      public function getGetfield()
      {
          return $this-&gt;getfield;
      }
  
      /**
       * Get postfields array (simple getter)
       * 
       * @return array $this-&gt;postfields
       */
      public function getPostfields()
      {
          return $this-&gt;postfields;
      }
  
      /**
       * Build the Oauth object using params set in construct and additionals
       * passed to this method. For v1.1, see: https://dev.twitter.com/docs/api/1.1
       * 
       * @param string $url The API url to use. Example: https://api.twitter.com/1.1/search/tweets.json
       * @param string $requestMethod Either POST or GET
       * @return \TwitterAPIExchange Instance of self for method chaining
       */
      public function buildOauth($url, $requestMethod)
      {
          if (strtolower($requestMethod) !== 'post' &amp;&amp; strtolower($requestMethod) !== 'get')
          {
              exit('Request method must be either POST or GET');
          }
  
          $consumer_key = $this-&gt;consumer_key;
          $consumer_secret = $this-&gt;consumer_secret;
          $oauth_access_token = $this-&gt;oauth_access_token;
          $oauth_access_token_secret = $this-&gt;oauth_access_token_secret;
  
          $oauth = array( 
              'oauth_consumer_key' =&gt; $consumer_key,
              'oauth_nonce' =&gt; time(),
              'oauth_signature_method' =&gt; 'HMAC-SHA1',
              'oauth_token' =&gt; $oauth_access_token,
              'oauth_timestamp' =&gt; time(),
              'oauth_version' =&gt; '1.0'
          );
  
          $getfield = $this-&gt;getGetfield();
  
          if (!is_null($getfield))
          {
              $getfields = str_replace('?', '', explode('&amp;', $getfield));
              foreach ($getfields as $g)
              {
                  $split = explode('=', $g);
                  $oauth[$split[0]] = $split[1];
              }
          }
  
          $base_info = $this-&gt;buildBaseString($url, $requestMethod, $oauth);
          $composite_key = rawurlencode($consumer_secret) . '&amp;' . rawurlencode($oauth_access_token_secret);
          $oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
          $oauth['oauth_signature'] = $oauth_signature;
  
          $this-&gt;url = $url;
          $this-&gt;oauth = $oauth;
  
          return $this;
      }
  
      /**
       * Perform the acual data retrieval from the API
       * 
       * @param boolean optional $return If true, returns data. 
       * @return json If $return param is true, returns json data.
       */
      public function performRequest($return = true)
      {
          if (!is_bool($return)) 
          { 
              exit('performRequest parameter must be true or false'); 
          }
  
          $header = array($this-&gt;buildAuthorizationHeader($this-&gt;oauth), 'Expect:');
  
          $getfield = $this-&gt;getGetfield();
          $postfields = $this-&gt;getPostfields();
  
          $options = array( 
              CURLOPT_HTTPHEADER =&gt; $header,
              CURLOPT_HEADER =&gt; false,
              CURLOPT_URL =&gt; $this-&gt;url,
              CURLOPT_RETURNTRANSFER =&gt; true,
              CURLOPT_SSL_VERIFYPEER =&gt; false
          );
  
          if (!is_null($postfields))
          {
              $options[CURLOPT_POSTFIELDS] = $postfields;
          }
          else
          {
              if ($getfield !== '')
              {
                  $options[CURLOPT_URL] .= $getfield;
              }
          }
  
          $feed = curl_init();
          curl_setopt_array($feed, $options);
          $json = curl_exec($feed);
          curl_close($feed);
  
          if ($return) { return $json; }
      }
  
      /**
       * Private method to generate the base string used by cURL
       * 
       * @param string $baseURI
       * @param string $method
       * @param string $params
       * @return string Built base string
       */
      private function buildBaseString($baseURI, $method, $params) 
      {
          $return = array();
          ksort($params);
  
          foreach($params as $key=&gt;$value)
          {
              $return[] = "$key=" . $value;
          }
  
          return $method . "&amp;" . rawurlencode($baseURI) . '&amp;' . rawurlencode(implode('&amp;', $return)); 
      }
  
      /**
       * Private method to generate authorization header used by cURL
       * 
       * @param array $oauth Array of oauth data generated by buildOauth()
       * @return string $return Header used by cURL for request
       */    
      private function buildAuthorizationHeader($oauth) 
      {
          $return = 'Authorization: OAuth ';
          $values = array();
  
          foreach($oauth as $key =&gt; $value)
          {
              $values[] = "$key=\"" . rawurlencode($value) . "\"";
          }
          $return .= implode(', ', $values);
          return $return;
      }
  
  }
  </code></pre>