-4

I want to pass a string from one PHP file to another using $_GET method. This string has different value each time it is being passed. As I understand, you pass GET parameters over a URL and you have to explicitly tell what the parameter is. What if you want to return whatever the string value is from providing server to server requesting it? I want to pass in json data format. Additionally how do I send it as Ajax?

Server (get.php):

<?php
$tagID = '123456';       //this is different every time
$tag = array('tagID' => $_GET['tagID']);
echo json_encode($tag);
?>

Server (rec.php):

<?php
$url = "http://192.168.12.169/RFID2/get.php?tagID=".$tagID;

$json = file_get_contents($url);
#var_dump($json);

$data = json_decode($json);
#var_dump($data);

echo $data;
?>
  • 1
    For AJAX, check the [MDN](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest). For the $_GET variable, just iterate through it, as it's an array. A `foreach($_GET as $k => $v) { // code }` will do it. It's not recommended though – Artemis Apr 12 '17 at 16:04
  • Is this question really that stupid? I do not have enough knowledge about _GET method, I just want to pass a string from one server to another, I'm sorry for my question but I don't even know where to start. –  Apr 12 '17 at 16:12
  • 1
    A small check on the [PHP documentation over `$_GET`](https://secure.php.net/manual/en/reserved.variables.get.php) shows that the `$_GET` variable is a key>value array (known as a dict in, for example, python). That means you can simply iterate through it as an array – Artemis Apr 12 '17 at 16:14
  • 1
    query strings are essentially *key*, value pairs. provide a known key and the value can be anything – Steve Apr 12 '17 at 16:15
  • You probably got so many downvotes because your code appears to work fine - did you try it? If yes, what happened? How does that differ from you desired behavior? – Steve Apr 12 '17 at 16:17
  • @Steve this is the error: Notice: Undefined variable: tagId in C:\wamp\www\Brompton\RFID2\rec.php on line 3 –  Apr 12 '17 at 16:21
  • I will improve my code slightly. To be key value as I had it before. –  Apr 12 '17 at 16:22
  • You are fighting the inherent nature of how these things work. There are several elements that are bad form or problematic in your code. If passing form server to server lean away from GET as that data will be much more visible in the logs (security risk?). And FWIW, the down votes are possibly because you are not performing Ajax IMO. The "j" is for Javascript, not seeing any... – Marc Apr 13 '17 at 00:12

1 Answers1

0

If I understand correctly, you want to get the tagID from the server? You can simply pass a 'request' parameter to the server that tells the server what to return.

EDIT: This really isn't the proper way to implement an API (like, at all), but for the sake of answering your question, this is how:

Server

switch($_GET['request']) {

   case 'tagID';
        echo json_encode($tag);
        break;
}

You can now get the tagID with a URL like 192.168.12.169/get.php?request=tagId

Client (PHP with CURL)

When it comes to the client it gets a bit more complicated. You mention AJAX, but that will only work for JavaScript. Your php file can't use AJAX, you'll have to use cURL.

$request = "?request=tagID";
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, '192.168.12.169/get.php' . $request); 

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, '3');
$content = trim(curl_exec($ch));
curl_close($ch);
echo $content;

EDIT: added the working cURL example just for completeness.

Included cURL example from: How to switch from POST to GET in PHP CURL

Client (Javascript with AJAX)

$.get("192.168.12.169/get.php?request=tagId", function(data) {
    alert(data);
});
Community
  • 1
  • 1
  • Okay my mistake, I treat it as a client but actually it is connection between two servers, I will correct my question. –  Apr 12 '17 at 16:26
  • 1
    @UZIERSKI Your use of the term client is actually correct in this context. Between two PHP servers, regardless of what you call them, you have to use cURL. –  Apr 12 '17 at 16:32
  • I mentioned about AJAX as I will want to wrap this tagID in a JavaScript but I think I should not mention that, as for now I want to learn how client(server) can request variable value from server. –  Apr 12 '17 at 16:37
  • 1
    Do you mean wrap in JSON? This can actually be done with `json_encode` (added to example). –  Apr 12 '17 at 16:39
  • yes, thank you, I will try this code later, as currently I'm away from machine. Is it possible for you to send me a link to a simple Ajax in Javascript requesting the value as you did with cURL? –  Apr 12 '17 at 16:44
  • @UZIERSKI Sure thing. Keep in mind this is all just 'to get it working'. A lot is missing for a production environment. –  Apr 12 '17 at 16:56
  • I understand that a lot is missing from client side? I can handle that, you helped me a lot already –  Apr 12 '17 at 16:58
  • If I want to pass more parameters through Switch statement is it better to convert it to if statement and place && between each one or is there any neater way to do it? –  Apr 12 '17 at 17:01
  • But then I would need to change it to POST method as there is a limit on GET method –  Apr 12 '17 at 17:04
  • Or use something like $_SERVER['QUERY-STRING'] –  Apr 12 '17 at 17:06
  • @UZIERSKI This is getting a little more complicated. [Switch statements can be stacked](http://stackoverflow.com/questions/4163188/php-switch-case-more-than-1-value-in-the-case). If you use SWITCH or IF is up to you. If you want to use POST and GET, you can use `$_REQUEST` instead, it holds either. –  Apr 12 '17 at 17:10