0

I have created a php file called "brain.php" that takes a get parameter of "message" - so for example "brain.php?msg=hello". It will respond with a JSON array that can be handled by the application.

I have built a JQuery app that can make these requests, and now I'm attempting to do it in PHP but I'm not sure how.

The following code does not work as it thinks the parameter is part of the filename

$response = file_get_contents("../brain.php?msg=hello");
echo $response;

The following code kind of works but simply responds with the entirety of the code instead of the response

$response = file_get_contents("../brain.php");
echo $response;

What is the best way to make the request with the ?msg variable and store the JSON response in a variable for handling?

Thank you!

6
  • 1
    Research 'cURL requests' in php. That should give you an idea on where to start. Commented Apr 4, 2018 at 19:54
  • 1
    If these 2 files are always to live on the same server, you should probably do this via file inclusion or by calling whatever function in brain.php generates the output. You do not need to make an HTTP call into brain.php. Commented Apr 4, 2018 at 19:55
  • Read php.net/manual/en/function.file-get-contents.php Commented Apr 4, 2018 at 19:55
  • If not file_get_contents("http://example.com/brain.php?msg=hello") Commented Apr 4, 2018 at 19:55
  • Can yiou post the contents of brain.php (abbreviated to only the relevant parts) ? we may be able to suggest ways to restructure it to make it easy to use both from external js calls and internally from the same PHP application. Commented Apr 4, 2018 at 19:56

2 Answers 2

1

You can get content from URL using file_get_contents:

$response  = file_get_contents('https://httpbin.org/ip?test=test');
$jsonData = json_decode($response, true));

However you need to check if allow_url_fopen is enabled in your php.ini. Alternatively you can do the same with curl:

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://httpbin.org/ip?test=test');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$jsonData = json_decode(curl_exec($curl), true);

curl_close($curl);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! CURL was exactly what I was looking for, I've never done decoupled request handling before so this was quite new to me. Extremely useful and I'll use this a lot in future! Thank you very much.
0

First of all, I have to say: you're probably doing this wrong.

This should work better:

  1. In your file, set the variable msg (e.g: $msg = "Hello")
  2. Import the file using include_once('../brain.php')
  3. In brain.php, set the JSON response (or another type) in a variable (e.g: $foo = json_encode(...)) and make necessary adjustments to read $msg variable (e.g.: $_GET['msg'] to $msg)
  4. After including brain.php, use your "new" variable as you wish.

1 Comment

That would work if the .php file that responds with the JSON array is staying locally but it will be stored on a server elsewhere

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.