0
votes

This is the code I use for sending notifications to my app.

    include("connection.php");
    include("utils.php");

    $Info = mysql_fetch_array(mysql_query("SELECT * FROM data WHERE iddata = ".$_GET["dataId"]));
    $ownerInfo = mysql_fetch_array(mysql_query("SELECT * FROM dataUsers WHERE id = ".$dataInfo['id']));
    $data = mysql_fetch_array(mysql_query("SELECT dataUsers.id, dataUsers.token, data.idData, data.id FROM data INNER JOIN data ON data.iddata = '".$_GET["data"]."' AND data.id = dataUsers.id"))["token"];

    $datagOwnerDeviceToken = getDeviceTokenFromUserToken($dataOwnerToken);
    //call server where simplePush.php is located -ports opened-
    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_URL => "http://www.myserver.com/sendNotif.php?msg=hello guys&deviceToken=xxxxxxxxxxxx",
        CURLOPT_USERAGENT => 'whatever'
        ));

        $resp = curl_exec($curl);
        curl_close($curl);

The problem is that I receive the notification but instead of "Hello guys", I receive "Hello", that's to say, the string rips off when it founds a blank space. I think it could be due to the encoding but I don't really know how to make it work.

If I change the msg parameter to msg=helloguys I receive to whole string. So there's a problem with blank spaces.

Many thanks.

This is now the actual code:

$curl = curl_init();

curl_setopt_array($curl, array( CURLOPT_RETURNTRANSFER => 1,

$url = "http://server.com/notifications.php?".http_build_query(array(
    'msg' => "hello this is ".$dataInfo["wastringText"],
    'deviceToken' => $dataOwnerDeviceToken
));

echo "This is my url: ".$url; curl_setopt_array($curl, array( CURLOPT_RETURNTRANSFER => 1, CURLOPT_URL => $url, CURLOPT_USERAGENT => 'whatever' ));
$resp = curl_exec($curl); curl_close($curl);

1
it may to help you $url = urlencode(YOUR_URL_STRING); CURLOPT_URL => $url - Sergey Belyakov
@SergeyBelyakov You should not use urlencode() on the whole URL, just each parameter. - Barmar
Still having issues placing the code in the labels. Someone who edit? - A. Sola

1 Answers

0
votes

You need to encode the space as %20 or +. But it would be best to use http_build_query to encode the whole query string.

$url = "http://www.myserver.com/sendNotif.php?" . 
    http_build_query(array(
        'msg' => 'hello guys',
        'deviceToken' => 'xxxxxxxxxxxx'
    ));
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => $url,
    CURLOPT_USERAGENT => 'whatever'
    ));