PHP code example of stefangabos / zebra_curl

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

    

stefangabos / zebra_curl example snippets






// include the library
// (you don't need this if you installed the library via Composer)
url->cache('path/to/cache', 3600);

// since we are communicating over HTTPS, we load the CA bundle from the examples folder,
// so we don't get CURLE_SSL_CACERT response from cURL
// you can always update this bundle from https://curl.se/docs/caextract.html
$curl->ssl(true, 2, __DIR__ . '/cacert.pem');

// a simple way of scraping a page
// (you can do more with the "get" method and callback functions)
echo $curl->scrape('https://github.com/', true);



// include the library
// (you don't need this if you installed the library via Composer)
url->cache('path/to/cache', 3600);

// since we are communicating over HTTPS, we load the CA bundle from the examples folder,
// so we don't get CURLE_SSL_CACERT response from cURL
// you can always update this bundle from https://curl.se/docs/caextract.html
$curl->ssl(true, 2, __DIR__ . '/cacert.pem');

$feeds = array(
    'https://rss1.smashingmagazine.com/feed/'       =>  'Smashing Magazine',
    'https://feeds.feedburner.com/nettuts'          =>  'TutsPlus',
    'https://feeds.feedburner.com/alistapart/main'  =>  'A List Apart',
);

// get RSS feeds of some popular tech websites
$curl->get(array_keys($feeds), function($result) use ($feeds) {

    // everything went well at cURL level
    if ($result->response[1] == CURLE_OK) {

        // if server responded with code 200 (meaning that everything went well)
        // see https://httpstatus.es/ for a list of possible response codes
        if ($result->info['http_code'] == 200) {

            // the content is an XML, process it
            $xml = simplexml_load_string($result->body);

            // different types of RSS feeds...
            if (isset($xml->channel->item))

                // show title and date for each entry
                foreach ($xml->channel->item as $entry) {
                    echo '<h6>' . $feeds[$result->info['original_url']] . '</h6>';
                    echo '<h2><a href="' . $entry->link . '">' . $entry->title . '</a></h2>';
                    echo '<p><small>' . $entry->pubDate . '</small></p>';
                    echo '<p>' . substr(strip_tags($entry->description), 0, 500) . '</p><hr>';
                }

            // different types of RSS feeds...
            else

                // show title and date for each entry
                foreach ($xml->entry as $entry) {
                    echo '<h6>' . $feeds[$result->info['original_url']] . '</h6>';
                    echo '<h2><a href="' . $entry->link['href'] . '">' . $entry->title . '</a></h2>';
                    echo '<p><small>' . $entry->updated . '</small></p>';
                    echo '<p>' . substr(strip_tags($entry->content), 0, 500) . '</p><hr>';
                }

        // show the server's response code
        } else trigger_error('Server responded with code ' . $result->info['http_code'], E_USER_ERROR);

    // something went wrong
    // ($result still contains all data that could be gathered)
    } else trigger_error('cURL responded with: ' . $result->response[0], E_USER_ERROR);

});

// include the library
// (you don't need this if you installed the library via Composer)
ng over HTTPS, we load the CA bundle from the examples folder,
// so we don't get CURLE_SSL_CACERT response from cURL
// you can always update this bundle from https://curl.se/docs/caextract.html
$curl->ssl(true, 2, __DIR__ . '/cacert.pem');

// set custom HTTP headers
$curl->option(CURLOPT_HTTPHEADER, [
    'accept: application/json',
    'X-Token-Foo-Bar: ABC123'   // Pass keys to APIs, for example
]);

echo $curl->scrape('https://httpbin.org/get') . PHP_EOL;



// include the library
// (you don't need this if you installed the library via Composer)
er HTTPS, we load the CA bundle from the examples folder,
// so we don't get CURLE_SSL_CACERT response from cURL
// you can always update this bundle from https://curl.se/docs/caextract.html
$curl->ssl(true, 2, __DIR__ . '/cacert.pem');

// download one of the official twitter image
$curl->download('https://abs.twimg.com/a/1362101114/images/resources/twitter-bird-callout.png', 'cache');