【问题标题】:Using $_GET in a PHP array()在 PHP 数组中使用 $_GET()
【发布时间】:2011-12-19 11:44:36
【问题描述】:

我有一个 PHP 数组:

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

$name 数组工作的唯一方法是,如果我使用诸如“名称”之类的字符串

$name = $_GET['name']; 不起作用。

如何正确地将$_GET 放入这个数组中?

这是我的全部代码...

<?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 -->

【问题讨论】:

  • 我不确定是什么原因造成的,但您是否尝试过:array("name" => $name... just $name without the quotes?虽然没关系
  • 编辑是的,未引用的变量确实有效。 $params = array("name" => $_GET['name'], "description" => "not applicable", "location" => "Orem Utah", "start_time" => "07/25/2013" , "end_time" => "07/26/2013", "privacy_type" => "OPEN");不起作用...我将如何实现这一目标?
  • @DavidEugenePeterson $params = array("name" =&gt; $_GET['name']... 在什么方面不起作用?错误信息等...?
  • 我正在使用:$params = array(name => $_GET['name'], "description" => "not applicable", "location" => "Orem Utah", " start_time" => "07/25/2013", "end_time" => "07/26/2013", "privacy_type" => "OPEN");但我只能提交包含 1. 一个单词 2. 没有符号的事件...................... ...................为什么@DaveRandom 背后有什么想法?

标签: php arrays get global params


【解决方案1】:
$params = array("name" => $name, "description" => "not applicable", "location" => "Orem Utah", "start_time" => "07/25/2013", "end_time" = >“2013 年 7 月 26 日”,“隐私类型”=>“开放”);

【讨论】:

    【解决方案2】:

    问题是你试图在变量中连接,就好像你正在构建一个 SQL 字符串或其他东西......

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

    你只需要在这里使用一个简单的变量:

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

    【讨论】:

      【解决方案3】:

      您需要从数组中取出字符串值。

      首先,是否设置了 GET?

      如果它在 URL 中,它看起来像这样: url.com/mypage.php?id=1

      您可以通过这种方式从 URL 中获取它。

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

      如果来自表单,请尝试:

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

      如果你有 get 设置,那么在你的数组中,你可以像这样设置它:

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

      您不需要使用引号。

      【讨论】:

      • 我试过你的方法,但是收到 [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 400 错误请求.....删除文件获取内容表明 (id) aka (name) 已正确注册到其相应的变量。但是,仍然没有发布到 Fbook。
      • 你欺骗了我们好先生。 :P 你让我们认为你是新手,但你的同类表明并非如此。
      • 我打赌你会得到 400,因为你无权获取该文件的文件内容(也就是读取)。我打赌该文件不存在。
      • 我将此行称为问题行。 $access_token = file_get_contents($token_url);我知道 $token_url 是一个 URL,但这并不意味着他可以阅读那里的内容。
      【解决方案4】:

      您是否检查过 $_GET 实际上传递了一个名为“名称”的变量?在您的开发框中,添加如下行:

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

      并检查是否确实通过了某些东西。

      我注意到的另一件事是你的报价有点搞砸了。你可以

      • 删除 $name 周围的引号:array("name"=&gt;$name...);
      • 如果需要,请添加正确的引号:

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

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

      最后,如果您要在数据库中使用传递的变量,请不要忘记转义它们:

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

      希望对你有帮助

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-11-16
        • 1970-01-01
        • 2011-11-04
        • 2010-12-22
        • 1970-01-01
        • 1970-01-01
        • 2018-12-17
        相关资源
        最近更新 更多