通过GET在URL中添加更多变量

时间:2014-06-02 16:45:00

标签: php url get google-cloud-messaging

我想有可能通过URL示例传递参数。

www.example.com/index.php?msg=textOne&var2=textTwo&var3=textThree. 

我需要更改代码才能使其正常工作?

<?php
define("GOOGLE_API_KEY", ...);
define("GOOGLE_GCM_URL", "https://android.googleapis.com/gcm/send");

function send_gcm_notify($reg_id, $message, $text) {

    $fields = array(
        'registration_ids'  => array( $reg_id ),
        'data'              => array( "message" => $message ),  
    );

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

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, GOOGLE_GCM_URL);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

    $result = curl_exec($ch);
    if ($result === FALSE) {
        die('Problem occurred: ' . curl_error($ch));
    }

    curl_close($ch);
    echo $result;
 }

$reg_id = "APA91bHuSGES.....nn5pWrrSz0dV63pg";
$msg    = filter_input (INPUT_GET, 'msg', FILTER_SANITIZE_STRING);
send_gcm_notify($reg_id, $msg,put);

1 个答案:

答案 0 :(得分:2)

如果要使用数组样式:

....
$getParams = array(
    'param1' => 'value',
    'param2' => 'value2',
);
$ch = curl_init();  
curl_setopt($ch, CURLOPT_URL, GOOGLE_GCM_URL . '?' . implode('&', array_map('urlencode', $getParams)));
curl_setopt($ch, CURLOPT_POST, true);
....

请注意urlencode的使用,以确保您的参数是网址安全的。