PHP code example of metaer / curl-wrapper-bundle

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

    

metaer / curl-wrapper-bundle example snippets


$connectionTimeout = 8; //seconds
$serverResponseTimeout = 20; //seconds

$options = [
    CURLOPT_CONNECTTIMEOUT => $connectionTimeout,
    CURLOPT_TIMEOUT => $serverResponseTimeout,
];

$cw->getQueryResult($options);
 php 
new \Metaer\CurlWrapperBundle\MetaerCurlWrapperBundle(),
 php
$options = [
    CURLOPT_URL => 'http://example.ex',
    CURLOPT_RETURNTRANSFER => true,
];
        
$cw = $container->get('metaer_curl_wrapper.curl_wrapper');

try {
    $result = $cw->getQueryResult($options);
} catch (CurlWrapperException $e) {
    //handle exception
}
 php
MyController {
    /**
     * @var CurlWrapper
     */
    private $curlWrapper;

    /**
     * MyController constructor.
     * @param CurlWrapper $curlWrapper
     */
    public function __construct(CurlWrapper $curlWrapper)
    {
        $this->curlWrapper = $curlWrapper;
    }
    
    public function myAction() {    
        $options = [
            CURLOPT_URL => 'http://example.ex',
            CURLOPT_RETURNTRANSFER => true,
        ];

        try {
            $this->curlWrapper->getQueryResult($options);
        } catch (CurlWrapperException $e) {
            //handle exception
        }
        
        return new Response('something');
    }
}
 php
MyController {
    public function myAction(CurlWrapper $curlWrapper) {    
        $options = [
            CURLOPT_URL => 'http://example.ex',
            CURLOPT_RETURNTRANSFER => true,
        ];

        try {
            $result = $curlWrapper->getQueryResult($options);
        } catch (CurlWrapperException $e) {
            //handle exception
        }
        
        return new Response('something');
    }
}
 php

namespace App\Service;

use Metaer\CurlWrapperBundle\CurlWrapper;
use Metaer\CurlWrapperBundle\CurlWrapperException;

class MyService
{
    /**
     * @var CurlWrapper
     */
    private $curlWrapper;
    
    /**
     * MyService constructor.
     * @param CurlWrapper $curlWrapper
     */
    public function __construct(CurlWrapper $curlWrapper)
    {
        $this->curlWrapper = $curlWrapper;
    }
    
    public function myMethod() {    
        $options = [
            CURLOPT_URL => 'http://example.ex',
            CURLOPT_RETURNTRANSFER => true,
        ];

        try {
            $result = $this->curlWrapper->getQueryResult($options);
        } catch (CurlWrapperException $e) {
            //handle exception
        }
    }
}
 php
// src/MyCurlWrapper.php
namespace App;

use Metaer\CurlWrapperBundle\CurlWrapper;

class MyCurlWrapper extends CurlWrapper
{
    public function getQueryResult(array $curlOptions)
    {
        //your code here
        return 'something';
    }
    
    public function myCustomMethod()
    {
        //something else
    }
}