Reputation: 5085
This is very basic, but I am kind of confused where I am going wrong (learning how to implement a RESTful Web Service). The context is, I have a simple simulator.php file that simulates an HTTP request to one of my local PHP files. The local PHP file (index.php) does nothing but return a variable with a value. So it's pretty much like this:
<?php
$variable = 'hello';
return $variable;
?>
and my simulator.php file has the following:
?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost/kixeye/index.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec($ch);
var_dump($contents);
curl_close($ch);
?>
However, var_dump($contents) does not quite spit out the value of $variable which is being returned from index.php. I don't quite understand why not.
Upvotes: 1
Views: 621
Reputation: 14730
The $contents
variable will contain the web page being returned by the http request done with Curl. If you only need one value from index.php
, just echo it, and its value will end up in $contents
as a string.
If you want to retrieve several variables, you could try json encode them and then echo the result in index.php
. Then you would have to do the reverse in your second script by json decoding $contents
.
Alternatively, you could generate and echo valid php code in the first script, and then eval it in the second, but this is very bad practice (the use of eval
is strongly discouraged).
See:
Upvotes: 0
Reputation: 75993
return
ing something outside of a function won't actually do anything. The cURL request you are making will return the HTML response from the requested page, so what your really want to do is echo
the response rather than using return
.
Just change the index.php
script to this:
<?php
$variable = 'hello';
echo $variable;
?>
And your var_dump()
in the second script will output hello
.
Upvotes: 3