PHP code example of haukurh / curl

1. Go to this page and download the library: Download haukurh/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/ */

    

haukurh / curl example snippets




aukurh\Curl\Curl;

$curl = new Curl();

$response = $curl->get("http://example.com/feed.xml");

if ($response->isOk()) {
    $xml = $response->body();
    // Do something with the XML
}

// Post something

$fields = [
    'title' => 'Some article title',
    'content' => 'Some silly example content',
];

$postResponse = $curl->post("http://example.com/article/new", $fields);

if ($response->isSuccessful()) {
    // Be happy you post request was successful
}

$curl = new Curl();

$response = $curl->get("http://example.com/feed.xml");

echo $response->url(); // "http://example.com/feed.xml"
echo $response->code(); // 200
echo $response->isOk(); // true
echo $response->contentType(); // text/xml;
echo $response->size(); // 3510

$body = $response->body();

$json = $response->json(); // Json payload as ?stdClass
$array = (array)$response->json(); // Json as an array


$curl = new Curl();

$curl->setTimeout(3);
$curl->setFollowLocation(true);
$curl->setUserAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36');

$response = $curl->get("http://example.com/feed.xml");

if ($response->isOk()) {
    $xml = $response->body();
    // Do something with the XML
}

$curl = new Curl([
    CURLOPT_TIMEOUT => 3,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_USERAGENT => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36',
]);

$response = $curl->get("http://example.com/feed.xml");

if ($response->isOk()) {
    $xml = $response->body();
    // Do something with the XML
}

$curl = new Curl();

$options = [
    CURLOPT_TIMEOUT => 3,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_USERAGENT => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36',
];

$response = $curl->request("http://example.com/feed.xml", $options);

if ($response->isOk()) {
    $xml = $response->body();
    // Do something with the XML
}