Saving a hang in php variable

I am trying to access the ec2 public hostname from inside the instance.

I would like to run this command

curl http:// 169 254.169.254/latest/meta-data/public-hostname 

inside the php script and save the response to the variable. How can i do this?

+7
source share
2 answers

You can do it like

 <?php //URL of targeted site $url = "http://www.yahoo.com/"; $ch = curl_init(); // set URL and other appropriate options curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // grab URL and pass it to the browser $output = curl_exec($ch); //echo $output; // close curl resource, and free up system resources curl_close($ch); ?> 

The $output variable contains the response.

+19
source

Shankar Damodaran provided an example of how to get a response from a curl request, but in particular it

CURLOPT_RETURNTRANSFER , which does as it says and returns the result from curl_exec

+11
source

All Articles