1. Go to this page and download the library: Download linslin/yii2-curl library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
linslin / yii2-curl example snippets
use linslin\yii2\curl;
$curl = new curl\Curl();
//get http://example.com/
$response = $curl->get('http://example.com/');
if ($curl->errorCode === null) {
echo $response;
} else {
// List of curl error codes here https://curl.haxx.se/libcurl/c/libcurl-errors.html
switch ($curl->errorCode) {
case 6:
//host unknown example
break;
}
}
// GET request with GET params
// http://example.com/?key=value&scondKey=secondValue
$curl = new curl\Curl();
$response = $curl->setGetParams([
'key' => 'value',
'secondKey' => 'secondValue'
])
->get('http://example.com/');
// POST URL form-urlencoded
$curl = new curl\Curl();
$response = $curl->setPostParams([
'key' => 'value',
'secondKey' => 'secondValue'
])
->post('http://example.com/');
// POST RAW JSON
$curl = new curl\Curl();
$response = $curl->setRawPostData(
json_encode[
'key' => 'value',
'secondKey' => 'secondValue'
])
->post('http://example.com/');
// POST RAW JSON and auto decode JSON respawn by setting raw = true.
// This is usefull if you expect an JSON response and want to autoparse it.
$curl = new curl\Curl();
$response = $curl->setRawPostData(
json_encode[
'key' => 'value',
'secondKey' => 'secondValue'
])
->post('http://example.com/', true);
// JSON decoded response by parsing raw = true in to ->post().
var_dump($response);
// POST RAW XML
$curl = new curl\Curl();
$response = $curl->setRawPostData('<?xml version="1.0" encoding="UTF-8"
// POST with special headers
$curl = new curl\Curl();
$response = $curl->setPostParams([
'key' => 'value',
'secondKey' => 'secondValue'
])
->setHeaders([
'Custom-Header' => 'user-b'
])
->post('http://example.com/');
// POST JSON with body string & special headers
$curl = new curl\Curl();
$params = [
'key' => 'value',
'secondKey' => 'secondValue'
];
$response = $curl->setRequestBody(json_encode($params))
->setHeaders([
'Content-Type' => 'application/json',
'Content-Length' => strlen(json_encode($params))
])
->post('http://example.com/');
// Avanced POST request with curl options & error handling
$curl = new curl\Curl();
$params = [
'key' => 'value',
'secondKey' => 'secondValue'
];
$response = $curl->setRequestBody(json_encode($params))
->setOption(CURLOPT_ENCODING, 'gzip')
->post('http://example.com/');
// List of status codes here http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
switch ($curl->responseCode) {
case 'timeout':
//timeout error logic here
break;
case 200:
//success logic here
break;
case 404:
//404 Error logic here
break;
}
//list response headers
var_dump($curl->responseHeaders);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.