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());

Thursday, 30 April 2015

How to delete the Zend Cache files from server


How to delete the zend cache files from server


How to create an object of Zend Cache
$cache = Zend_Cache::factory(
                        'Core', 'File', array(
                    'lifetime' => 3600 * 24 * 7, //cache is cleaned once a day                            
                    'automatic_serialization' => true
                        ), array('cache_dir' => APPLICATION_PATH . '/cache')
        );


Remove the cache by cache-id In this only one cache file will be removed from server.
$cache->remove('idToRemove');


Remove All cache files from Server. (When we delete files, caching automatic clear because we are using cache in files)
$cache->clean(Zend_Cache::CLEANING_MODE_ALL);


Remove all outdated caching files.
$cache->clean(Zend_Cache::CLEANING_MODE_OLD);


Remove the cahe files which have two tags.
$cache->clean(
    Zend_Cache::CLEANING_MODE_MATCHING_TAG,
    array('matchingTag1', 'matchingTag2','matchingTag3') //Will remove which have matchingTag1 and matchingTag2 and matchingTag3
);


Remove all files which have not tag assign few tags.
$cache->clean(
    Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG,
    array('NotmatchingTag1', 'NotmatchingTag2') //Will Remove all the cahce which are NOT tagged with NotmatchingTag1 OR NotmatchingTag2
);


Remove all the cache files which have any tag (any of two or three).
$cache->clean(
    Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG, //Will remove which have matchingTag1 OR matchingTag2 OR matchingTag3
     array('matchingTag1', 'matchingTag2','matchingTag3')
);