Showing posts with label CURL. Show all posts
Showing posts with label CURL. Show all posts

Thursday, 12 October 2017

Azure video indexer - Get text from the Video


Question: What is Video Indexer API?
This API is used to Get the Visual Text from the Video. It will give you all the text that appear in the video.



Question: How to Get Text from the video?
  1. Get the API key
  2. Get the VIdeo URL from Azure OR Amazon S3 Or any public video URL.
  3. Pass the Video URL with API key and get the video Id token.
  4. Pass the video Id token with API key and get the all Text



Question: How to Get the API Key?
Follow the below link, you will get the API key which can be used further.
https://docs.microsoft.com/en-us/azure/cognitive-services/video-indexer/video-indexer-use-apis#subscribe-to-the-api


Question: How to Get the videoId token from the video?
$subScriptionKey = 'AZURE_SUBSCRIPTION_KEY_HERE';   
$videoUrl='https://foldername.blob.core.windows.net/bucketname/GV-NNokn-989-5175_360p.mp4';

$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => "https://videobreakdown.azure-api.net/Breakdowns/Api/Partner/Breakdowns?name=new%20video%20test&privacy=Private&videoUrl=".urlencode($videoUrl),
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "",
  CURLOPT_HTTPHEADER => array(
    "cache-control: no-cache",
    "content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
    "ocp-apim-subscription-key: {$subScriptionKey}",    
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

OR Using Zend Framework
$subScriptionKey = 'AZURE_SUBSCRIPTION_KEY_HERE';   
$videoUrl='https://foldername.blob.core.windows.net/bucketname/GV-NNokn-989-5175_360p.mp4';
$jsonData = '';        
$config = array(
       'adapter' => 'Zend_Http_Client_Adapter_Curl',
       'curloptions' => array(CURLOPT_FOLLOWLOCATION => true,CURLOPT_SSL_VERIFYPEER =>FALSE),            
   );
    $url='https://videobreakdown.azure-api.net/Breakdowns/Api/Partner/Breakdowns';
   $client = new Zend_Http_Client($url, $config);
   //Set the Header value
    $client->setHeaders('Content-Type', "multipart/form-data");
    $client->setHeaders('Ocp-Apim-Subscription-Key', $subScriptionKey);

    $postData = array(
    'name'=>'GV-NNokn-989-5175',
    'privacy'=>'Private',
    'videoUrl'=>$videoUrl
    );
    $client->setParameterGet($postData);

   $response = $client->request('POST'); 
   $jsonData= ($response->getBody());



Output
9a10d9d0002



Question: How to get video text from videoId token?
$subScriptionKey = 'AZURE_SUBSCRIPTION_KEY_HERE';   
$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://videobreakdown.azure-api.net/Breakdowns/Api/Partner/Breakdowns/VIDEOIDTOKEN",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array(
    "cache-control: no-cache",
    "ocp-apim-subscription-key: {$subScriptionKey}",
    "postman-token: 43c1c192-1de6-ac3d-42e0-b1ddc7cc0ee0"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

OR Using Zend Framework
$subScriptionKey = 'AZURE_SUBSCRIPTION_KEY_HERE';   
        $language = 'English';
        $body = 'How to you?';
        $breadDownsId='2bc89c5ec1';

        //Main Data
        $client = new Zend_Http_Client('https://videobreakdown.azure-api.net/Breakdowns/Api/Partner/Breakdowns/' . $breadDownsId, array(
            'maxredirects' => 0,
            'timeout' => 30
        ));       
        
        $client->setHeaders('Ocp-Apim-Subscription-Key', $subScriptionKey);

        $parameters = array(
            'language' => $language,
        );
        $client->setParameterGet($parameters);

        try {
            $response = $client->request('GET');
            $data = $response->getBody();
        } catch (Exception $ex) {
            $data = $ex;
        }
        echo $response->getBody();die;

Output
{
    "accountId": "2b525a6-3a5408006294",
    "id": "9a10d9d912",
    "partition": null,
    "name": "new video test",
    "description": null,
    "userName": "Ram Kumar",
    "createTime": "2017-10-12T04:41:20.8986398+00:00",
    "organization": "",
    "privacyMode": "Private",
    "state": "Processed",
    "isOwned": true,
    "isEditable": false,
    "isBase": true,
    "durationInSeconds": 1373,
    "summarizedInsights":{/* It will have data*/},
    "breakdowns":{/* It will have data*/},
    "social":{/* It will have data*/}
}




Wednesday, 11 October 2017

How to get Speech Recognition from Audio OR Video

How to get Speech Recognition from Audio OR Video

Question: What does do the voicebase.com?
Give the Audio/Video URL in API and get all Speech in Text.


Question: How to get the API Key?
You can request API access using this form: http://info.voicebase.com/request-api-key


Question: What other it can do?
  1. Machine-generated Transcript
  2. Per Word Confidence
  3. Time-stamped words
  4. Player SDK
  5. Stereo speaker ID



Question: How to get media-id in voicebase api?
    //Get Token
    $tokenKey='eyJ0eXAiOiJKV1QiLCJhbGciOiJIUadDKFDSLJLDLJKJLKKWWUEUUEEUUEUEUUEUUEUDUUDUDDLDLDLLDLLLDLLDLDLKFMFKK2b2ljZWJhc2UuY29tIn0.8M4bmafBqdlqon-o65CMKabBjxVatDObzRklDoUysNU';

    //Get Video URL
    $videoAudioURL='http://example.com/audio/filename.mp3';

    $curl = curl_init();
    curl_setopt_array($curl, array(
      CURLOPT_URL => "https://apis.voicebase.com/v3/media",
      CURLOPT_RETURNTRANSFER => true,
        CURLOPT_SSL_VERIFYPEER=>false,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 30,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "POST",
      CURLOPT_POSTFIELDS => "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"mediaUrl\"\r\n\r\n{$videoAudioURL}\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--",
      CURLOPT_HTTPHEADER => array(
        "authorization: Bearer {$tokenKey}",
        "cache-control: no-cache",
        "content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",    
      ),
    ));

    $response = curl_exec($curl);
    $err = curl_error($curl);

    curl_close($curl);

    if ($err) {
      echo "cURL Error #:" . $err;
    } else {
      echo $response;
    }



Output
{
  "_links": {
    "self": {
      "href": "/v3/media/70cafaf7-89bc-474f-bb02-c59f0181a51f"
    },
    "progress": {
      "href": "/v3/media/70cafaf7-89bc-474f-bb02-c59f0181a51f/progress"
    },
    "metadata": {
      "href": "/v3/media/70cafaf7-89bc-474f-bb02-c59f0181a51f/metadata"
    }
  },
  "formatVersion": "3.0.1",
  "mediaId": "70cafaf7-89bc-474f-bb02-c59f0181a51f",
  "status": "accepted",
  "dateCreated": "2017-10-11T08:46:56.343Z",
  "metadata": {
    
  },
  "mediaContentType": "video/mp4",
  "length": 222956
}



Question: How to get transcription from mediaID?
        //Get Token
        $tokenKey='eyJ0eXAiOiJKV1QiLCJhbGciOiJIUadDKFDSLJLDLJKJLKKWWUEUUEEUUEUEUUEUUEUDUUDUDDLDLDLLDLLLDLLDLDLKFMFKK2b2ljZWJhc2UuY29tIn0.8M4bmafBqdlqon-o65CMKabBjxVatDObzRklDoUysNU';
        $mediaId='70cafaf7-89bc-474f-bb02-c59f0181a51f';

$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => "https://apis.voicebase.com/v3/media/{$mediaId}",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_POSTFIELDS => "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"mediaUrl\"\r\n\r\nhttp://enterprise-dev2.georama.com/test/love.mp3\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--",
  CURLOPT_HTTPHEADER => array(
    "authorization: Bearer {$tokenKey}",
    "cache-control: no-cache",
    "content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
    "postman-token: 0effcf6c-38cd-0ebc-dc87-3d821fd5ca72"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}
Output
{
  "confidence": 0.16606535003699963,
  "words": [
    {
      "p": 0,
      "c": 0.998,
      "s": 4230,
      "e": 4540,
      "w": "Hello"
    },
    {
      "p": 1,
      "c": 0.95,
      "s": 4549,
      "e": 4660,
      "w": "my"
    },
    {
      "p": 2,
      "c": 0.941,
      "s": 4670,
      "e": 4799,
      "w": "name"
    }
  ]
}

Tuesday, 10 October 2017

CURL Example with Zend Framework1

CURL Example with Zend Framework1

Question: Give CURL Example with parameter in POST Method in Zend framework?
$config = array(
    'adapter' => 'Zend_Http_Client_Adapter_Curl',
    'ssltransport' => 'tls',
    'strict' => false,
    'persistent' => true,
);
$url = 'http://www.example.com';
$client = new Zend_Http_Client($url, $config);
$postArray = array('name'=>'Georage','age'=>33);
$client->setParameterPost($postArray);
$response = $client->request('POST');
echo $response->getBody();



Question: How to set Header data in CURL with Zend Framework1?
$client->setHeaders(array(
    'Host: www.example.com',
    'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0',
    'Accept: text/javascript, application/javascript, */*',
    'Accept-Language: en-gb,en;q=0.5',
    'Accept-Encoding: gzip, deflate',
    'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
    'X-Requested-With: XMLHttpRequest',
    'Referer: http://example.com'
));


Question: How to set Cookie data in CURL with Zend Framework1?
$client->setCookie('__utma', '174057141.1507422797.1392357517.1392698281.1392702703.3');
$client->setCookie('PHPSESSID', '0a12ket5d8d7otlo6uh66p658a5');



Question: How to set Content-Type in CURL with Zend Framework1?
$client->setHeaders('Content-Type', 'audio/flac');



Question: Give CURL Example with different option in Zend framework?
$config = array(
    'adapter' => 'Zend_Http_Client_Adapter_Curl',
    'ssltransport' => 'tls',
    'strict' => false,
    'persistent' => true,
);
$url = 'http://www.example.com';
$client = new Zend_Http_Client($url, $config);
$postArray = array('name'=>'Georage','age'=>33);
$client->setParameterPost($postArray);
/** set Headers **/
$client->setHeaders(array(
    'Host: www.example.com',
    'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0',
    'Accept: text/javascript, application/javascript, */*',
    'Accept-Language: en-gb,en;q=0.5',
    'Accept-Encoding: gzip, deflate',
    'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
    'X-Requested-With: XMLHttpRequest',
    'Referer: http://example.com'
));

/** set cookie **/
$client->setCookie('__utma', '174057141.1507422797.1392357517.1392698281.1392702703.3');
$client->setCookie('PHPSESSID', '0a12ket5d8d7otlo6uh66p658a5');
$response = $client->request('POST');

$data= $response->getBody();
echo $data; 



Question: How to Skip SSL Check in CURL using Zend Framework?
$url='www.example.com';
$config = array(
    'adapter' => 'Zend_Http_Client_Adapter_Curl',
    'curloptions' => array(
        CURLOPT_FOLLOWLOCATION => TRUE,
        CURLOPT_SSL_VERIFYPEER => FALSE
    ),
);
 $client = new Zend_Http_Client($url, $config);
 $response = $client->request('GET');



Question: How to pass binary data of in CURL usig Zend Framework?
$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());

Saturday, 14 December 2013

CURL Example

CURL
CURL is a library and command-line tool for transferring data using various protocols like HTTP, FTP, SCP, PUT, POST, POP3 & SMTP etc. It also used for scrapping the data from websites. We can scrap get the text as well as images with use of CURL.


It have two products libcurl and cURL.

cURL: Its command line tool for transferring information.
Libcurl: It is a free client-side URL transfer library.


CURL COMMAND-LINE EXAMPLE

Curl Example with "GET" Method (By Default GET)
curl http://www.web-technology-experts-notes.in/p/sitemap.html

Curl Example with "POST" Method
curl --request POST http://www.web-technology-experts-notes.in/p/sitemap.html

Curl Example with "PUT" Method
curl --request PUT http://www.web-technology-experts-notes.in/p/sitemap.html

Curl Example with "DELETE" Method
curl --request DELETE http://www.web-technology-experts-notes.in/p/sitemap.html

Curl Example with "POST" Method and pass data (Data: username, password & logintype)
curl --request POST http://www.web-technology-experts-notes.in/p/sitemap.html
--data 'username=user&password=****&logintype=normal' 

Curl Example with "POST" Method and pass data(data in text file)
curl --request POST http://www.web-technology-experts-notes.in/p/sitemap.html --data @datafile.txt

Curl Example with "POST" Method, pass data and pass header
curl --request POST http://www.web-technology-experts-notes.in/p/sitemap.html --data @datafile.txt --header 'sessionid:9874563211235884' 

Curl Example with "POST" Method, pass data and pass header (Get Header in response) --include
curl --request POST http://www.web-technology-experts-notes.in/p/sitemap.html --data @datafile.txt --header 'sessionid:9874563211235884' --include

CURL LIBCURL EXAMPLE

Mostly to get data from an REST API, we use following method in php
echo file_get_contents('http://www.web-technology-experts-notes.in/p/sitemap.html');
But if, "allow_url_fopen" is disable in php.ini file due to security reason then above will not work. You need to use below code
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.google.co.in");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$output = curl_exec($ch);
curl_close($ch);
echo $output;die;


CURL Example with Post Method with parameter
$url = "http://localhost/test/index2";
$post_data = array(
            "foo" => "bar",
            "foo1" => "testdata"
);
try {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    $output = curl_exec($ch);
    curl_close($ch);
} catch (Exception $e) {
    echo $e->getMessage();die;
}

File Upload with CURL in PHP
 $file_name_with_full_path = realpath('uploadFile.zip');        
        $url = "http://localhost/test/index2";
        $post_data = array(
            "foo" => "bar",
            "upload" => "@".$file_name_with_full_path
        );
        try {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
            $output = curl_exec($ch);
            curl_close($ch);
        } catch (Exception $e) {
            echo $e->getMessage();
            die;
        }
        
Transfer the File through FTP
curl_setopt_array($ch, array(
    CURLOPT_URL => 'ftp://ftp.example.com/test.txt',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_USERPWD => 'username:password'
));
 
$output = curl_exec($ch);

Set Header and Json data in Curl
$url='http://www.web-technology-experts-notes.in/';
$jsonData='{"name":"arun","email":"[email protected]"}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  'X-API-Token: ' . X_API_TOKEN,
  'Content-Type: application/json',
  "HTTP_X_FORWARDED_FOR: xxx.xxx.x.xx"
));

curl_setopt($ch, CURLOPT_INTERFACE, "xxx.xxx.x.xx");
echo curl_exec($ch);
curl_close($ch);
Following are some Options which can be set be set in "curl_setopt" function


CURLOPT_REFERER - Set the Http Refer

CURLOPT_USERAGENT - Set the UserAgent

CURLOPT_HEADER - Include the header in response or not

CURLOPT_RETURNTRANSFER - Print the output or not

CURLOPT_URL - Set the URL (Uniform Resource Locator)

CURLOPT_POST - set 0 or 1 for post method

CURLOPT_POSTFIELDS - set the data which you want to post

CURLOPT_FOLLOWLOCATION - if set true, cURL will follow redirects

CURLOPT_CONNECTTIMEOUT -Total seconds to spend attempting to connect

CURLOPT_TIMEOUT - Total Seconds to allow cURL to execute






Zend Curl Example

$config = array(
    'adapter' => 'Zend_Http_Client_Adapter_Curl',
    'ssltransport' => 'tls',
    'strict' => false,
    'persistent' => true,
);
$url = 'http://www.example.com';
$client = new Zend_Http_Client($url, $config);
$postArray = array('name'=>'Georage','age'=>33);
$client->setParameterPost($postArray);
/** set Headers **/
$client->setHeaders(array(
    'Host: www.example.com',
    'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0',
    'Accept: text/javascript, application/javascript, */*',
    'Accept-Language: en-gb,en;q=0.5',
    'Accept-Encoding: gzip, deflate',
    'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
    'X-Requested-With: XMLHttpRequest',
    'Referer: http://example.com'
));

/** set cookie **/
$client->setCookie('__utma', '174057141.1507422797.1392357517.1392698281.1392702703.3');
$client->setCookie('__utmz', '174057141.1392357517.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)');
$client->setCookie('__atuvc', '58');
$client->setCookie('PHPSESSID', '0a12ket5d8d7otlo6uh66p658a5');
$response = $client->request('POST');

$data= $response->getBody();
echo $data; 

Zend CURL Ignore SSL_VERIFYPEER
$url='www.example.com';
$config = array(
    'adapter' => 'Zend_Http_Client_Adapter_Curl',
    'curloptions' => array(
        CURLOPT_FOLLOWLOCATION => TRUE,
        CURLOPT_SSL_VERIFYPEER => FALSE
    ),
);
 $client = new Zend_Http_Client($url, $config);
 $response = $client->request('GET');

Set CURL'S Timeout in Milli-seconds
 $ch = curl_init();
          //set the curl for crawl
          curl_setopt($ch, CURLOPT_URL, "http://www.web-technology-experts-notes.in/2014/03/captcha-code-example.html");
          //include the header in the output
          curl_setopt($ch, CURLOPT_HEADER, 0);
          //The number of seconds to wait while trying to connect. Use 0 to wait indefinitely.
          curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
          //The maximum number of milliseconds to allow cURL functions to execute.
          curl_setopt($ch, CURLOPT_TIMEOUT_MS, 4000); //timeout in seconds
          $output = curl_exec($ch);
          curl_close($ch); 

Set Zend CURL'S Timeout in Milli-seconds
$config = array(
            'adapter' => 'Zend_Http_Client_Adapter_Curl',
            'curloptions' => array(
                CURLOPT_FOLLOWLOCATION => FALSE,
                CURLOPT_HEADER => false,
                //The number of seconds to wait while trying to connect. Use 0 to wait indefinitely.
                CURLOPT_TIMEOUT_MS => 3000,
                //The maximum number of milliseconds to allow cURL functions to execute.
                CURLOPT_CONNECTTIMEOUT_MS => 3000
            )
        );
        $url = "http://www.web-technology-experts-notes.in/2014/03/captcha-code-example.html";
        $client = new Zend_Http_Client($url, $config);
        $response = $client->request('GET');
        $output = $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 data(file) in CURL ?
curl -X POST -u "USERNAME":"PASSWORD" --header "Content-Type: audio/flac" --header "Transfer-Encoding: chunked" --data-binary @audio.flac "https://stream.watsonplatform.net/speech-to-text/api/v1/recognize" 



Question: How to store linux command output in file?
curl -X POST -u "USERNAME":"PASSWORD" --header "Content-Type: audio/flac" --header "Transfer-Encoding: chunked" --data-binary @audio.flac "https://stream.watsonplatform.net/speech-to-text/api/v1/recognize" > output1.txt