将curl响应保存到php变量中 [英] Saving a curl response into a php variable
本文介绍了将curl响应保存到php变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从实例内部访问ec2的公共主机名。
I am trying to access my ec2's public hostname from inside the instance.
我想运行此命令
curl http:// 169 254.169.254/latest/meta-data/public-hostname
在php脚本中,并将响应保存到变量中。我该怎么做?
inside a php script and save the response to a variable. How can I do 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);
?>
$ output
变量包含响应。
这篇关于将curl响应保存到php变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文