Saving a curl response into a php variable

后端 未结 2 1189
隐瞒了意图╮
隐瞒了意图╮ 2021-02-07 18:41

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/         


        
相关标签:
2条回答
  • 2021-02-07 19:08

    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.

    0 讨论(0)
  • 2021-02-07 19:28

    Shankar Damodaran provided an example of how to retrieve the response from a curl request, but specifically it is the

    CURLOPT_RETURNTRANSFER that does as it says and returns the result from curl_exec

    0 讨论(0)
提交回复
热议问题