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';
        }
        


Wednesday 10 July 2019

How to upload very large file in S3 using PHP?

How to upload very large file in S3 using PHP?

Step 1: Install AWS S3 Library (If not installed)


composer require aws/aws-sdk-php


Step 2: Get Access key/Secret Access Key (If you have not)


  1. Open the IAM console (https://console.aws.amazon.com/iam/home?#home).
  2. From the navigation menu, click Users.
  3. Select your IAM user name.
  4. Click User Actions, and then click Manage Access Keys.
  5. Click Create Access Key.
  6. Your keys will look something like this:
    Access key ID example: AKARUNKODNN7EXAMPLE Secret access key example: wJaXUtFDADnFEMI/K7MDDEG/bPxRfiDDDEXAMPLEKEY


Step 3:Upload the files


require 'vendor/autoload.php';

use Aws\Common\Exception\MultipartUploadException;
use Aws\S3\MultipartUploader;
use Aws\S3\S3Client;
$myAwsKey='AWS_ACCESS_KEY';
myAwsSecretKey='AWS_SECRET_KEY';
 
$s3Media='s3-bucket-name';
$s3FileName='filename.mp4';

//Init S3 Object
try {
    $s3 = new S3Client([
        'version' => 'latest',
        'region'  => 'us-east-1',
        'credentials' => [
            'key'    => $myAwsKey,
            'secret' => $myAwsSecretKey,
        ]                
    ]);
} catch (MultipartUploadException $e) {
        echo $e->getMessage() . PHP_EOL;
        die;
} 

//Start uploading
try{
    $uploader = new MultipartUploader($s3, $staticFilePath.'/GM-hKStd-1211-28453_720p.mp4', 
        array(
        'bucket' => $s3Media,
        'key'    => $s3FileName,
        'ACL'    => 'public-read',
    )
    );                
}  catch (Exception $e){
    pr($e->getMessage());
}


// Perform the upload.
try {
    $result = $uploader->upload();
    //It will give you complete path of S3 URL
    echo "Upload complete: {$result['ObjectURL']}" . PHP_EOL;
} catch (MultipartUploadException $e) {
    echo $e->getMessage() . PHP_EOL;
}