1

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 -->
4
  • I'm not sure what's causing this, but have you tried: array("name" => $name... just $name without the quotes? though it shouldn't matter Commented Dec 19, 2011 at 11:49
  • EDIT yes unquoted varibles do work. $params = array("name" => $_GET['name'], "description" => "not applicable", "location" => "Orem Utah", "start_time" => "07/25/2013", "end_time" => "07/26/2013", "privacy_type" => "OPEN"); does not work... how would I achieve this? Commented Dec 19, 2011 at 12:29
  • 1
    @DavidEugenePeterson In what way does $params = array("name" => $_GET['name']... not work? Error message etc...? Commented Dec 19, 2011 at 13:53
  • I'm using: $params = array(name => $_GET['name'], "description" => "not applicable", "location" => "Orem Utah", "start_time" => "07/25/2013", "end_time" => "07/26/2013", "privacy_type" => "OPEN"); but i'm only able to submit events that contain 1. One word 2. No symbols .................................................. any thoughts behind why @DaveRandom? Commented Dec 19, 2011 at 16:09

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

Comments

1

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'],...

Comments

1

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.

4 Comments

I tried your method, but am recieving [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request.....removing file get contents shows that (id) aka (name) is registered properly to its corresponding varible. However, still not posting to Fbook.
You deceived us good sir. :P You made us think you were novice, but your kind indicates otherwise.
I am betting you're getting the 400 because you don't have permission to get file contents (aka read) on that file. I'm betting the file doesn't exist.
I'm referencing this line as the problem line. $access_token = file_get_contents($token_url); I know $token_url is a URL but that doesn't mean he can read the contents there.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.