Laravel -- Curl 远程访问的使用

xiaoxiao2021-02-28  82

前提需要php 安装 curl 扩展

curl GET 方法

$ch = curl_init();//初始化curl curl_setopt($ch, CURLOPT_URL, $url);//设置url属性 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); $output = curl_exec($ch);//获取数据 curl_close($ch);//关闭curl return $output;

curl POST 方法

$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_POSTFIELDS, $post_data); $output = curl_exec($ch); curl_close($ch); return $output;

如果你post和get同时使用 那就封装成一个函数来使用吧~

public function curlGet($url,$method,$post_data = 0){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); if ($method == 'post') { curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); }elseif($method == 'get'){ curl_setopt($ch, CURLOPT_HEADER, 0); } $output = curl_exec($ch); curl_close($ch); return $output; }
转载请注明原文地址: https://www.6miu.com/read-29857.html

最新回复(0)