Cara menggunakan firebase storage php

function MultiandroidNotification($deviceToken, $message,$type,$title=null,$sub_title=null,$device_type=null,$data = null,$content_available = null)
{

    if($content_available == 1){
        $content_available = false;
    }else{
        $content_available =  true;
    }
    if($type == 12 || $type == 13){
        $priority = '';
    }else{
        $priority = 'high';
    }

    $deviceToken = array_values($deviceToken);
    $no = null;

    $apiKey = 'XXXXXXXXXXXXXXXXXXXXXX';

    $notification = array("text" => "test",'badge' => "1",'sound' => 'shutter.wav','body'=>$message,'icon'=>'notif_icn','title'=>$title,'priority'=>'high','tag'=>'test','vibrate'=> 1,'alert'=> $message);

    $msg = array('message'=> $message,'title'=> $title,'sub_title'=> $sub_title,'type'=>$type,'activitydata' => $data);

    if($device_type == 'Android'){
        $fields = array("content_available" => true,"priority" => "high",'registration_ids'=> $deviceToken,'data'=> $msg);
    }else{
        $fields = array('notification' => $notification,"content_available" => $content_available,"priority" => $priority,'registration_ids'=> $deviceToken,'data'=> $msg);
    }

    $headers = array('Authorization: key=' . $apiKey,'Content-Type: application/json');

    if ($headers){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://fcm.googleapis.com/fcm/send");
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
        $response = curl_exec($ch);
    }
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        if (curl_errno($ch)) {
            return false; //probably you want to return false
        }
        if ($httpCode != 200) {
            return false; //probably you want to return false
        }
        curl_close($ch);
        return $response;
}

I have a php application and from this i need upload images to google firebase storage. I had seen snippets that write to and read from realtime database but couldnt find any code that upload from php to firebase storage.

Any help will be much appreciated.

Tismon Varghese

asked Jul 4, 2016 at 11:38

Cara menggunakan firebase storage php

You can get started by using their official documentation at The storage documentation and An easier to use SDK within a Framework You can explore the two for other information. So to get started on how I did mine. I was working on a laravel project though.

You have to install this package composer require kreait/firebase-php

Then also read the setup steps at Package Setup

     $storage = app('firebase.storage'); // This is an instance of Google\Cloud\Storage\StorageClient from kreait/firebase-php library
     $defaultBucket = $storage->getBucket();
     $image = $request->file('image');
     $name = (string) Str::uuid().".".$image->getClientOriginalExtension(); // use Illuminate\Support\Str;
     $pathName = $image->getPathName();
     $file = fopen($pathName, 'r');
     $object = $defaultBucket->upload($file, [
          'name' => $name,
          'predefinedAcl' => 'publicRead'
     ]);
     $image_url = 'https://storage.googleapis.com/'.env('FIREBASE_PROJECT_ID').'.appspot.com/'.$name;

I hope this helps.

answered Feb 21, 2021 at 16:37

Cara menggunakan firebase storage php

2mighty2mighty

1392 silver badges5 bronze badges

We recommend using the Google API Client PHP library for this, as Firebase Storage is backed by Google Cloud Storage. Firebase Storage doesn't provide a PHP client for front end code though.

From the GCS PHP example code:

// composer autoloading
require_once __DIR__ . '/vendor/autoload.php';

// grab the first argument
if (empty($argv[1])) {
    die("usage: php listBuckets [project_id]\n");
}

$projectId = $argv[1];

// Authenticate your API Client
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->addScope(Google_Service_Storage::DEVSTORAGE_FULL_CONTROL);

$storage = new Google_Service_Storage($client);

/**
 * Google Cloud Storage API request to retrieve the list of buckets in your project.
 */
$buckets = $storage->buckets->listBuckets($projectId);

foreach ($buckets['items'] as $bucket) {
    printf("%s\n", $bucket->getName());
}

answered Jul 4, 2016 at 17:10

Cara menggunakan firebase storage php

Mike McDonaldMike McDonald

15.1k2 gold badges44 silver badges47 bronze badges

2