0

Trying to retrieve data from firebase, but getting error. Please help me for setup this.

My Code is

function Process($url){
    $Return = new stdClass;

    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_HEADER, 0);  // remove header 
    curl_setopt($ch, CURLOPT_NOBODY, 0); // remove body 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');

    if($Return->response = curl_exec($ch)){
        $Return->response = json_decode($Return->response);
    }

    $Return->httpCode   = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $Return->curl_errno = curl_errno($ch);
    $Return->curl_error = curl_error($ch); 

    curl_close($ch); 
    return $Return;
}

$Info = Process('https://project-id.firebaseio.com/users/jack/name.json');

but getting response Unauthorized request.

2
  • curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain')); add this Commented Aug 16, 2018 at 10:07
  • I try this, my question is how can i set API or access token ? Commented Aug 16, 2018 at 10:08

1 Answer 1

1

You need to add your authorization key into the header section of your curl. The server key or token depends on what information you are trying to retrieve from the firebase.

if its personal information about the account you can see the example on this link https://firebase.google.com/docs/database/rest/auth

if you are trying to retrieve information for a certain project then this server key can be found within the project console in firebase.

$server_key ='YOUR SERVER KEY';
$headers = array(
    'Content-Type:application/json',
  'Authorization:key='.$server_key
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
Sign up to request clarification or add additional context in comments.

2 Comments

I got the server key from settings>Cloud Messaging Tab>Server Key but still Permission denied.
What information are you trying to grab? Each section has different validation. are you trying to send notification to registered devices?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.