Saturday 2 May 2015

How to set authorization header in Curl using zend framework

 How to set authorization header in Curl using zend framework

 How to set authorization header in Curl using Zend Framework
   $url ='www.domain.com/web-service-API';
  $config = array(
        'adapter' => 'Zend_Http_Client_Adapter_Curl',
        'curloptions' => array(CURLOPT_FOLLOWLOCATION => true,CURLOPT_SSL_VERIFYPEER =>FALSE),            
    );
    
    $client = new Zend_Http_Client($url, $config);
    //Set the Header value
    $client->setAuth('username', 'password!');
    
    $response = $client->request('GET'); 
    echo $response->getBody();



Set header (Name & Value) in curl using Zend Framework
$url ='www.domain.com/web-service-API';
  $config = array(
        'adapter' => 'Zend_Http_Client_Adapter_Curl',
        'curloptions' => array(CURLOPT_FOLLOWLOCATION => true,CURLOPT_SSL_VERIFYPEER =>FALSE),            
    );
    
    $client = new Zend_Http_Client($url, $config);
    //Set the Header value
    $client->setHeaders('HEADER-NAME', 'Header-Value');
    
    $response = $client->request('GET'); //Here you can set GET Method Also
    echo $response->getBody();   



Send the JSON Data in curl using Zend Framework
$url ='www.domain.com/web-service-API';
$json = json_encode(array('name'=>'Web','age'=>20));
  $config = array(
        'adapter' => 'Zend_Http_Client_Adapter_Curl',
        'curloptions' => array(CURLOPT_FOLLOWLOCATION => true,CURLOPT_SSL_VERIFYPEER =>FALSE),            
    );
    $response = $client->setRawData($json, 'application/json')->request('POST');
    echo $response->getBody();




Question: How to Set the username/password in CURL?
curl -X POST -u "USERNAME":"PASSWORD" --header "Content-Type: audio/flac" --header "Transfer-Encoding: chunked"  "https://stream.watsonplatform.net/speech-to-text/api/v1/recognize" 




Question: How to pass binary User Authentication (Username/password) in CURL ?
$file='http://example.com/test/GM-qE2zq-1078-5525_360p.flac';
$jsonData = '';        
$config = array(
       'adapter' => 'Zend_Http_Client_Adapter_Curl',
       'curloptions' => array(CURLOPT_FOLLOWLOCATION => true,CURLOPT_SSL_VERIFYPEER =>FALSE),            
   );
    $url='https://stream.watsonplatform.net/speech-to-text/api/v1/recognize';
   $client = new Zend_Http_Client($url, $config);
   //Set the Header value
        $client->setHeaders('Content-Type', 'audio/flac');
    $client->setHeaders('Transfer-Encoding', 'chunked');            

   $client->setRawData(file_get_contents($file));
    //Set post method    
   $response = $client->request('POST'); 
   $jsonData= ($response->getBody());