在PHP数组中使用$ _GET()

I have a PHP array:

$params = array(
  "name" => "$name",
  "description" => "not applicable", 
  "location" => "Orem Utah",
  "start_time" => "07/25/2013",
  "end_time" => "07/26/2013",
  "privacy_type" => "OPEN"
); 

The only way I can get the $name array to work, is if I use strings such as "name"

$name = $_GET['name']; Does not work.

How do I properly put $_GET into this array?

Here's my entire code...

<?php

  session_start(); 
  $app_id = "xxxxxxx";
  $app_secret = "xxxxxxxx";
  $my_url = "http://www.xxxxxxxx.xxxx/xxxxxxxxxevent.php?name=".urlencode($_SESSION['name']).""; 

  $code = $_REQUEST["code"];

  if (empty($code)) {
    $auth_url = "http://www.facebook.com/dialog/oauth?client_id="
    . $app_id . "&redirect_uri=" . urlencode($my_url)
    . "&scope=create_event";
    echo("<script>top.location.href='" . $auth_url . "'</script>");
  }

  $token_url = "https://graph.facebook.com/oauth/access_token?client_id="
  . $app_id . "&redirect_uri=" . urlencode($my_url)
  . "&client_secret=" . $app_secret
  . "&code=" . $code;
  $access_token = file_get_contents($token_url);

  $test = "$_GET[name]";
  $url = "https://graph.facebook.com/me/events?" . $access_token;
  $params = array(
    "name" => "$name",
    "description" => "not applicable", 
    "location" => "Orem Utah",
    "start_time" => "07/25/2013",
    "end_time" => "07/26/2013",
    "privacy_type" => "OPEN"
  ); 

  // Check if we have an image

  // Start the Graph API call
  $ch = curl_init();

  curl_setopt($ch, CURLOPT_URL,$url);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $params);

  $result = curl_exec($ch);
  $decoded = json_decode($result, 1);
  curl_close($ch);

  if (is_array($decoded) && isset($decoded['id'])) {
    $msg = "success";
  }
  $event_url = "https://graph.facebook.com/me/".$_SESSION['fid']."?" . $access_token;
  if (isset($msg)) {
    $_SESSION['fid'] = $decoded['id']; header("location:xxxxxx2.php");
  }

?>
<!-- irrelevant HTML BELOW HERE -->
$params = array("name" => $name, "description" => "not applicable", "location" => "Orem Utah", "start_time" => "07/25/2013", "end_time" => "07/26/2013", "privacy_type" => "OPEN");

The problem is that you're attempting to concatenate in the variable as though you were building a SQL string or something...

$params = array("name" => '".$name.'",...
//-----------------------^^^^^^^^^^^^^

You need to be just using a simple variable here:

$params = array("name" => $_GET['name'],...

You need to get the string value out of the array.

First, is the GET being set?

If it is in the URL, it would look like this: url.com/mypage.php?id=1

You can get it out of the URL this way.

$id = strval( $_GET['id'] );
echo 'ID: '.$id;  // check if it worked

If it is from a form, try:

echo '<pre>'; print_r( $_GET ); echo '</pre>';
$name = $_GET['name'];  // If you can see it in the print_r, then this should work

If you have the get set, then in your array, you can set it like:

$myArray = array( 'id' => $id, 'name' => "Names" );

You do not need to use quotes.

Have you checked that $_GET is actually passing a variable called 'name'? On your development box, add a line like:

echo '<pre>'.print_r($_GET,true).'<pre>';

and check that something is actually being passed.

The other thing I noticed is that your quotes are a bit screwed. You can either

  • remove the quotes around $name : array("name"=>$name...);
  • add the correct quotes if you need them :

    array("name"=>'"'.$name.'"'...)
    

    or

    array("name"=>"'{$name}'"...)
    

Finally, don't forget to escape the passed variables if you're going to use them in a database:

$name=mysql_escape_string($_GET['name']);

Hope this helps