Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving a curl response into a php variable

I am trying to access my ec2's 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 a php script and save the response to a variable. How can I do this?

like image 551
slick1537 Avatar asked May 02 '13 12:05

slick1537


People also ask

How can get cURL value in PHP?

PHP cURL GET requestphp $ch = curl_init('http://webcode.me'); curl_exec($ch); curl_close($ch); In the example, we send a GET request to a small website. The output is directly shown in the standard output.

Can I use cURL in PHP?

cURL is a PHP extension that allows you to use the URL syntax to receive and submit data. cURL makes it simple to connect between various websites and domains.

How get cURL URL in PHP?

Use curl_getinfo($ch) , and the first element ( url ) would indicate the effective URL.


1 Answers

You can do like this

<?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.

like image 121
Shankar Narayana Damodaran Avatar answered Sep 28 '22 10:09

Shankar Narayana Damodaran