php 请求远程链接
php 请求远程链接一般情况下解析别人的接口时经常用到,一般我们会想到用file_get_contents这个函数,但是次函数的效率比较低,如果大范围使用的话可能会造成页面卡顿的现象,所以比较理想的方法是用curl,下面我简单的分享一下:
一、curl get 请求:
$url = '127.0.0.1/test.php';//换成你要请求的url地址即可。//初始化一个 cURL 对象$ch = curl_init();//设置你需要抓取的URLcurl_setopt($ch, CURLOPT_URL, $url);// 设置cURL 参数,要求结果保存到字符串中还是输出到屏幕上。curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//是否获得跳转后的页面curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);$data = curl_exec($ch);curl_close($ch);echo $data;二、curl post 请求:
function curl_post($url, $arr_data){ $post_data = http_build_query($url_data); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFLELDS, $post_data); $data = curl_exec($ch); curl_close($ch); echo $data;}$arr_post = array( 'name'=>'test_name', 'age' => 1);curl_post("http://www.explame.com/", $arr_post);curl,也经常用于采集网页数据时使用。

浙公网安备 33010602011771号