cURL没有用PHP显示整个GET参数

时间:2015-12-09 20:09:55

标签: php curl encoding get

这是我用来向我的应用发送通知的代码。

    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);

问题是我收到了通知,但我收到“你好”,而不是“你好”,我收到“你好”,也就是说,当它找到一个空格时,字符串会被删除。我认为这可能是由于编码,但我真的不知道如何使其工作。

如果我将msg参数更改为msg = helloguys,我会收到整个字符串。所以空格有问题。

非常感谢。

现在是实际代码:

$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
));
回声“这是我的网址:”。$ url; curl_setopt_array($ curl,array(     CURLOPT_RETURNTRANSFER => 1,     CURLOPT_URL => $网址,     CURLOPT_USERAGENT => '随你'     ));
$ resp = curl_exec($ curl); curl_close($卷曲);

1 个答案:

答案 0 :(得分:0)

您需要将空格编码为%20+。但最好使用http_build_query对整个查询字符串进行编码。

$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'
    ));