Sunday 4 August 2019

How to post raw data using CURL in Zend Framework 2

How to post raw data using CURL in Zend Framework 2

Question: How to post raw data using Zend?
        $client = new Client('http://example.com/ajax/get-feedback-status', array(
            'maxredirects' => 0,
            'timeout' => 30
        ));

        // Performing a POST request
        $client->setMethod('POST');
        //Set data
        $client->setParameterGet(array(
            'first_name' => 'Web',
            'middle_name' => 'Technology',
            'last_name' => 'Experts',
            'category' => 'notes',
        ));
        $response = $client->send();
        if ($response->isSuccess()) {
            // the POST was successful
            echo $response->getBody();
        } else {
            echo 'Request is failed';
        }





Question: Can we post Multi dimensional array in Zend?
Yes, See example below.
        $client = new Client('http://example.com/ajax/get-feedback-status', array(
            'maxredirects' => 0,
            'timeout' => 30
        ));

        // Performing a POST request
        $client->setMethod('POST');
        //Set data
        $client->setParameterGet(array(
            'first_name' => 'Web',
            'middle_name' => 'Technology',
            'last_name' => 'Experts',            
            'post-ids' => array(10, 20, 30)
        ));



Question: How to add Cookie in httpClient Object?
        $client = new Client('http://example.com/ajax/get-feedback-status', array(
            'maxredirects' => 0,
            'timeout' => 30
        ));
        $cookieString = \Zend\Http\Header\SetCookie::fromString('Set-Cookie: flavor=chocolate%20chips');
        $client->addCookie($cookieString);



Question: How to change the HTTPS transport layer in httpClient Request?
        $config = array(
            'adapter' => 'Zend\Http\Client\Adapter\Socket',
            'ssltransport' => 'tls'
        );

        $client = new Client('http://example.com/ajax/get-feedback-status', $config);
        $cookieString = \Zend\Http\Header\SetCookie::fromString('Set-Cookie: flavor=chocolate%20chips');
        $client->addCookie($cookieString);



Question: How to call curl From proxy server?
        $config = array(
            'adapter'    => 'Zend\Http\Client\Adapter\Proxy',
            'proxy_host' => 'ADD HERE POST',
            'proxy_port' => 8000,
            'proxy_user' => 'USERNAME',
            'proxy_pass' => 'PASSWORD'
        );

        $client = new Client('http://example.com/ajax/get-feedback-status', $config);
        
        $client->setMethod('GET');        
        
        $response = $client->send();
        
    if ($response->isSuccess()) {
            // the POST was successful
            echo $response->getBody();
        } else {
            echo 'Request is failed';
        }