Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>class Curl_lib { private $resource = NULL; // libcurb init() resource private $config = array(); // Construction config public $header = array(); // Response Header public $body = array(); // Response Body /** * Factory Method */ public static function factory($data = array()) { return new self($data); } /** * Constructor */ public function __construct($data = array()) { $config = array( CURLOPT_HEADER =&gt; false ); // Apply any passed configuration $data += $config; $this-&gt;config = $data; $this-&gt;resource = curl_init(); // Apply configuration settings foreach ($this-&gt;config as $key =&gt; $value) { $this-&gt;set_opt($key, $value); } } /** * Set option * * @param String Curl option to set * @param String Value for option * @chainable */ public function set_opt($key, $value) { curl_setopt($this-&gt;resource, $key, $value); return $this; } /** * Execute the curl request and return the response * * @return String Returned output from the requested resource * @throws Kohana_User_Exception */ public function exec() { $ret = curl_exec($this-&gt;resource); //Wrap the error reporting in an exception if ($ret === false) { kohana::log('error', curl_error($this-&gt;resource)); throw new Exception(curl_error($this-&gt;resource)); } else { return $ret; } } /** * Get Error * Returns any current error for the curl request * * @return string The error */ public function get_error() { return curl_error($this-&gt;resource); } /** * Destructor */ function __destruct() { curl_close($this-&gt;resource); } /** * Get * Execute an HTTP GET request using curl * * @param String url to request * @param Array additional headers to send in the request * @param Bool flag to return only the headers * @param Array Additional curl options to instantiate curl with * @return Array array of 'header' and 'body' */ public static function get($url, Array $headers = array(), $headers_only = FALSE, Array $curl_options = array()) { $ch = self::factory($curl_options); $ch-&gt;set_opt(CURLOPT_URL, $url) -&gt;set_opt(CURLOPT_RETURNTRANSFER, TRUE) -&gt;set_opt(CURLOPT_NOBODY, $headers_only) -&gt;set_opt(CURLOPT_HTTPHEADER, array("Expect:")) -&gt;set_opt(CURLOPT_HEADERFUNCTION, array($ch, 'read_header')) -&gt;set_opt(CURLOPT_WRITEFUNCTION, array($ch, 'read_body')); //Set any additional headers if(!empty($headers)) $ch-&gt;set_opt(CURLOPT_HTTPHEADER, $headers); $ch-&gt;exec(); return array( 'header' =&gt; $ch-&gt;header, 'body' =&gt; $ch-&gt;body ); } /** * Post * Execute an HTTP POST request, posting the past parameters * * @param String url to request * @param Array past data to post to $url * @param Array additional headers to send in the request * @param Bool flag to return only the headers * @param Array Additional curl options to instantiate curl with * @return Array array of 'header' and 'body' */ public static function post($url, Array $data = array(), Array $headers = array(), $headers_only = FALSE, Array $curl_options = array()) { $ch = self::factory($curl_options); $ch-&gt;set_opt(CURLOPT_URL, $url) -&gt;set_opt(CURLOPT_NOBODY, $headers_only) -&gt;set_opt(CURLOPT_RETURNTRANSFER, TRUE) -&gt;set_opt(CURLOPT_POST, TRUE) -&gt;set_opt(CURLOPT_POSTFIELDS, $data) -&gt;set_opt(CURLOPT_HTTPHEADER, array("Expect:")) -&gt;set_opt(CURLOPT_HEADERFUNCTION, array($ch, 'read_header')) -&gt;set_opt(CURLOPT_WRITEFUNCTION, array($ch, 'read_body')); //Set any additional headers if(!empty($headers)) $ch-&gt;set_opt(CURLOPT_HTTPHEADER, $headers); $ch-&gt;exec(); return array( 'header' =&gt; $ch-&gt;header, 'body' =&gt; $ch-&gt;body ); } /** * Read Header * A private method to be used by libcurl when reading header. * * @param String Curl Binded Resource * @param String Header String * @return Integer Header String Length */ private function read_header($ch, $string) { $length = strlen($string); // Trim Header String $string = trim($string); // If not empty, push into header array if (!empty($string)) { array_push($this-&gt;header, $string); } return $length; } /** * Read Body * A private method to be used by libcurl when reading body content. * * @param String Curl Binded Resource * @param String Body String * @return Integer Body String Length */ private function read_body($ch, $string) { $length = strlen($string); // If not empty, push into body array if (!empty($string)) { array_push($this-&gt;body, $string); } return $length; } } </code></pre> <p>With the class above, what you need to do is just: </p> <pre><code>$response = Curl_lib::post('http://localhost/example.php', $post); </code></pre> <p>The <strong>$post</strong> as HTTP POST params and values to be posted to <a href="http://localhost/example.php" rel="nofollow noreferrer">http://localhost/example.php</a>'. <br /> It can be $_POST too, as long as an associative array will do. <br /></p> <p>The response is in the format of:</p> <pre><code>$response = array( 'header' =&gt; array(), 'body' =&gt; array(), ); </code></pre> <p>you can always use <strong>var_dump</strong> with <strong>xdebug</strong> to have a great view. <strong>print_r</strong> will is great too. Hope this help :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload