Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Gabotronix's avatar

My method is not returning curl result, returns pased parameters instead

In my laravel app I use curl to send notifications via FCM (Firebase Cloud Messaging) rest API using curl and php, when I execute curl I save result to result variable along with http code and return it, however what I receive in $notification variable is the parametters passed to NewMatchMotif instead of curl result:

$notification = new NewMatchNotif($user, $data, 'es');

Something like this:

{user: {…}, data: {…}, locale: "es"}

My NewMatch Notification:

<?php
namespace App\Notifications;

use App\Helpers\CloudMessaging;
use Lang;
use Log;

class NewMatchNotif
{
    
    public $user;
    public $data;
    public $locale;

    public function __construct($user, $data, $locale)
    {
        $this->user = $user;
        $this->data = $data;
        $this->locale = $locale;
        $this->load();
    }

    public function load()
    {
        $cloudMessaging = new CloudMessaging();
        $result = $cloudMessaging->send($this->user, 'title', 'body', $this->data, 'NewMatchNotif');
        return $result;
    }
    

}

My send method

public function send($user,$title,$body, $data = false , $type, $image='')
    { 
        $fields = '...';
        $url = 'https://fcm.googleapis.com/fcm/send';
        $fields = json_encode($fields);
    
        $headers = 
        [
            'Authorization: key='. config('services.firebase.api_key'),
            'Content-Type: application/json',
        ];	

        $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_POSTFIELDS, $fields );
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_NOBODY  , true); 
    
        $result = curl_exec ( $ch );
        $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

        curl_close ( $ch );

        $output = 
        [
            'httpCode' => $httpcode,
            'result' => $result
        ];
        
        return $output;
    }

What is going on, this part is pretty straight fordward yet I'm not getting the desired results:

 $result = curl_exec ( $ch );
        $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

        curl_close ( $ch );

        $output = 
        [
            'httpCode' => $httpcode,
            'result' => $result
        ];
        
        return $output;
0 likes
1 reply
Snapey's avatar
Snapey
Best Answer
Level 122

You shouldn't be trying to return a value from the constructor. remove $this->load() from the constructor and call it separately.

$newMatchMotif =  new NewMatchNotif($user, $data, 'es');

$notification = $newMatchMotif->load();

Please or to participate in this conversation.