合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
# php封装POST或者GET请求 php封装POST或者GET请求 ``` <pre class="calibre14">``` <span class="token"><</span><span class="token">?</span>php namespace app\common\controller<span class="token1">;</span> use think\Controller<span class="token1">;</span> <span class="token2">class</span> <span class="token3">Common</span> <span class="token2">extends</span> <span class="token3">Controller</span> <span class="token1">{</span> <span class="token6">/** * @封装get请求 */</span> <span class="token2">public</span> <span class="token2">function</span> <span class="token3">request_get</span><span class="token1">(</span>$url <span class="token">=</span> <span class="token4">''</span><span class="token1">,</span>$i<span class="token1">,</span>$token<span class="token1">)</span> <span class="token1">{</span> # code<span class="token">...</span> <span class="token2">if</span> <span class="token1">(</span><span class="token3">empty</span><span class="token1">(</span>$url<span class="token1">)</span><span class="token1">)</span> <span class="token1">{</span> <span class="token2">return</span> <span class="token5">false</span><span class="token1">;</span> <span class="token1">}</span> $headers <span class="token">=</span> <span class="token3">array</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> $headers<span class="token1">[</span><span class="token1">]</span><span class="token">=</span> <span class="token4">'Authorization: Bearer '</span><span class="token1">.</span>$token<span class="token1">;</span> $headers<span class="token1">[</span><span class="token1">]</span><span class="token">=</span> <span class="token4">'Connection: keep-alive'</span><span class="token1">;</span> $headers<span class="token1">[</span><span class="token1">]</span> <span class="token">=</span> <span class="token4">'Content-Type: application/json'</span><span class="token1">;</span> $headers<span class="token1">[</span><span class="token1">]</span> <span class="token">=</span> <span class="token4">'Referer: http://kao9357.cc/member'</span><span class="token1">;</span> $headers<span class="token1">[</span><span class="token1">]</span> <span class="token">=</span> <span class="token4">'User-Agent: Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Mobile Safari/537.36'</span><span class="token1">;</span> $ch <span class="token">=</span> <span class="token3">curl_init</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">//初始化init方法</span> <span class="token3">curl_setopt</span><span class="token1">(</span>$ch<span class="token1">,</span> <span class="token5">CURLOPT_RETURNTRANSFER</span><span class="token1">,</span> <span class="token5">1</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">//设定请求返回结果</span> <span class="token3">curl_setopt</span><span class="token1">(</span>$ch<span class="token1">,</span> <span class="token5">CURLOPT_TIMEOUT</span><span class="token1">,</span> <span class="token5">30</span><span class="token1">)</span><span class="token1">;</span><span class="token6">//设置超时时间</span> <span class="token3">curl_setopt</span><span class="token1">(</span>$ch<span class="token1">,</span> <span class="token5">CURLOPT_SSL_VERIFYPEER</span><span class="token1">,</span> <span class="token5">false</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">//忽略证书</span> <span class="token3">curl_setopt</span><span class="token1">(</span>$ch<span class="token1">,</span> <span class="token5">CURLOPT_SSL_VERIFYHOST</span><span class="token1">,</span> <span class="token5">false</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">//忽略证书</span> <span class="token3">curl_setopt</span><span class="token1">(</span>$ch<span class="token1">,</span> <span class="token5">CURLOPT_SSLVERSION</span><span class="token1">,</span> <span class="token5">1</span><span class="token1">)</span><span class="token1">;</span><span class="token6">//忽略证书</span> <span class="token3">curl_setopt</span><span class="token1">(</span>$ch<span class="token1">,</span> <span class="token5">CURLOPT_URL</span><span class="token1">,</span> $url<span class="token1">)</span><span class="token1">;</span> <span class="token6">//指定url</span> <span class="token3">curl_setopt</span><span class="token1">(</span>$ch<span class="token1">,</span> <span class="token5">CURLOPT_COOKIEFILE</span><span class="token1">,</span> $i<span class="token1">.</span><span class="token4">'.txt'</span><span class="token1">)</span><span class="token1">;</span><span class="token6">//使用cookie</span> $data <span class="token">=</span> <span class="token3">curl_exec</span><span class="token1">(</span>$ch<span class="token1">)</span><span class="token1">;</span> <span class="token6">//发送请求</span> <span class="token3">curl_close</span><span class="token1">(</span>$ch<span class="token1">)</span><span class="token1">;</span> <span class="token6">//关闭curl</span> <span class="token2">return</span> <span class="token3">json_decode</span><span class="token1">(</span>$data<span class="token1">,</span> <span class="token5">true</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">//返回数据</span> <span class="token1">}</span> <span class="token6">/** * @封装登录post * url 请求地址 * param 提交数据 * i 存放cookies文件序号 */</span> <span class="token2">public</span> <span class="token2">function</span> <span class="token3">request_post</span><span class="token1">(</span>$url <span class="token">=</span> <span class="token4">''</span><span class="token1">,</span> $param <span class="token">=</span> <span class="token4">''</span><span class="token1">,</span> $i<span class="token1">)</span> <span class="token1">{</span> <span class="token2">if</span> <span class="token1">(</span><span class="token3">empty</span><span class="token1">(</span>$url<span class="token1">)</span> <span class="token">||</span> <span class="token3">empty</span><span class="token1">(</span>$param<span class="token1">)</span><span class="token1">)</span> <span class="token1">{</span> <span class="token2">return</span> <span class="token5">false</span><span class="token1">;</span> <span class="token1">}</span> $ch <span class="token">=</span> <span class="token3">curl_init</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">//初始化init方法</span> <span class="token3">curl_setopt</span><span class="token1">(</span>$ch<span class="token1">,</span> <span class="token5">CURLOPT_URL</span><span class="token1">,</span> $url<span class="token1">)</span><span class="token1">;</span> <span class="token6">//指定url</span> <span class="token3">curl_setopt</span><span class="token1">(</span>$ch<span class="token1">,</span> <span class="token5">CURLOPT_SSL_VERIFYPEER</span><span class="token1">,</span> <span class="token5">false</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">//忽略证书</span> <span class="token3">curl_setopt</span><span class="token1">(</span>$ch<span class="token1">,</span> <span class="token5">CURLOPT_SSL_VERIFYHOST</span><span class="token1">,</span> <span class="token5">false</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">//忽略证书</span> <span class="token3">curl_setopt</span><span class="token1">(</span>$ch<span class="token1">,</span> <span class="token5">CURLOPT_SSLVERSION</span><span class="token1">,</span> <span class="token5">1</span><span class="token1">)</span><span class="token1">;</span><span class="token6">//忽略证书</span> <span class="token3">curl_setopt</span><span class="token1">(</span>$ch<span class="token1">,</span> <span class="token5">CURLOPT_HEADER</span><span class="token1">,</span> <span class="token5">0</span><span class="token1">)</span><span class="token1">;</span><span class="token6">//显示返回的header区域内容</span> <span class="token3">curl_setopt</span><span class="token1">(</span>$ch<span class="token1">,</span> <span class="token5">CURLOPT_TIMEOUT</span><span class="token1">,</span> <span class="token5">30</span><span class="token1">)</span><span class="token1">;</span><span class="token6">//设置超时时间</span> <span class="token3">curl_setopt</span><span class="token1">(</span>$ch<span class="token1">,</span> <span class="token5">CURLOPT_RETURNTRANSFER</span><span class="token1">,</span> <span class="token5">1</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">//设定请求返回结果</span> <span class="token3">curl_setopt</span><span class="token1">(</span>$ch<span class="token1">,</span> <span class="token5">CURLOPT_POST</span><span class="token1">,</span> <span class="token5">1</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">//声明使用post方式进行发送</span> <span class="token3">curl_setopt</span><span class="token1">(</span>$ch<span class="token1">,</span> <span class="token5">CURLOPT_POSTFIELDS</span><span class="token1">,</span> $param<span class="token1">)</span><span class="token1">;</span> <span class="token6">//post发送什么数据</span> <span class="token3">curl_setopt</span><span class="token1">(</span>$ch<span class="token1">,</span> <span class="token5">CURLOPT_COOKIEJAR</span><span class="token1">,</span> $i<span class="token1">.</span><span class="token4">'.txt'</span><span class="token1">)</span><span class="token1">;</span> $data <span class="token">=</span> <span class="token3">curl_exec</span><span class="token1">(</span>$ch<span class="token1">)</span><span class="token1">;</span> <span class="token6">//发送请求</span> <span class="token3">curl_close</span><span class="token1">(</span>$ch<span class="token1">)</span><span class="token1">;</span> <span class="token6">//关闭curl</span> <span class="token2">return</span> <span class="token3">json_decode</span><span class="token1">(</span>$data<span class="token1">,</span> <span class="token5">true</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">//返回数据</span> <span class="token6">//return $data;</span> <span class="token1">}</span> <span class="token6">/** * @自定义协议头的请求 */</span> <span class="token2">public</span> <span class="token2">function</span> <span class="token3">request_postop</span><span class="token1">(</span>$url <span class="token">=</span> <span class="token4">''</span><span class="token1">,</span> $param <span class="token">=</span> <span class="token4">''</span><span class="token1">,</span> $i<span class="token1">,</span> $token<span class="token1">)</span> <span class="token1">{</span> <span class="token2">if</span> <span class="token1">(</span><span class="token3">empty</span><span class="token1">(</span>$url<span class="token1">)</span> <span class="token">||</span> <span class="token3">empty</span><span class="token1">(</span>$param<span class="token1">)</span><span class="token1">)</span> <span class="token1">{</span> <span class="token2">return</span> <span class="token5">false</span><span class="token1">;</span> <span class="token1">}</span> $headers <span class="token">=</span> <span class="token3">array</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> $headers<span class="token1">[</span><span class="token1">]</span><span class="token">=</span> <span class="token4">'Authorization: Bearer '</span><span class="token1">.</span>$token<span class="token1">;</span> $headers<span class="token1">[</span><span class="token1">]</span><span class="token">=</span> <span class="token4">'Connection: keep-alive'</span><span class="token1">;</span> $headers<span class="token1">[</span><span class="token1">]</span> <span class="token">=</span> <span class="token4">'Content-Type: application/json'</span><span class="token1">;</span> $headers<span class="token1">[</span><span class="token1">]</span> <span class="token">=</span> <span class="token4">'Referer: http://kao9357.cc/member'</span><span class="token1">;</span> $headers<span class="token1">[</span><span class="token1">]</span> <span class="token">=</span> <span class="token4">'User-Agent: Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Mobile Safari/537.36'</span><span class="token1">;</span> $ch <span class="token">=</span> <span class="token3">curl_init</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">//初始化init方法</span> <span class="token3">curl_setopt</span><span class="token1">(</span>$ch<span class="token1">,</span> <span class="token5">CURLOPT_URL</span><span class="token1">,</span> $url<span class="token1">)</span><span class="token1">;</span> <span class="token6">//指定url</span> <span class="token3">curl_setopt</span><span class="token1">(</span>$ch<span class="token1">,</span> <span class="token5">CURLOPT_SSL_VERIFYPEER</span><span class="token1">,</span> <span class="token5">false</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">//忽略证书</span> <span class="token3">curl_setopt</span><span class="token1">(</span>$ch<span class="token1">,</span> <span class="token5">CURLOPT_SSL_VERIFYHOST</span><span class="token1">,</span> <span class="token5">false</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">//忽略证书</span> <span class="token3">curl_setopt</span><span class="token1">(</span>$ch<span class="token1">,</span> <span class="token5">CURLOPT_SSLVERSION</span><span class="token1">,</span> <span class="token5">1</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">//忽略证书</span> <span class="token3">curl_setopt</span><span class="token1">(</span>$ch<span class="token1">,</span> <span class="token5">CURLOPT_HEADER</span><span class="token1">,</span> <span class="token5">0</span><span class="token1">)</span><span class="token1">;</span> <span class="token3">curl_setopt</span><span class="token1">(</span>$ch<span class="token1">,</span> <span class="token5">CURLOPT_TIMEOUT</span><span class="token1">,</span> <span class="token5">30</span><span class="token1">)</span><span class="token1">;</span><span class="token6">//设置超时时间</span> <span class="token3">curl_setopt</span><span class="token1">(</span>$ch<span class="token1">,</span> <span class="token5">CURLOPT_RETURNTRANSFER</span><span class="token1">,</span> <span class="token5">1</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">//设定请求返回结果</span> <span class="token3">curl_setopt</span><span class="token1">(</span>$ch<span class="token1">,</span> <span class="token5">CURLOPT_POST</span><span class="token1">,</span> <span class="token5">1</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">//声明使用post方式进行发送</span> <span class="token3">curl_setopt</span><span class="token1">(</span>$ch<span class="token1">,</span> <span class="token5">CURLOPT_POSTFIELDS</span><span class="token1">,</span> $param<span class="token1">)</span><span class="token1">;</span> <span class="token6">//post发送什么数据</span> <span class="token3">curl_setopt</span><span class="token1">(</span>$ch<span class="token1">,</span> <span class="token5">CURLOPT_COOKIEFILE</span><span class="token1">,</span> $i<span class="token1">.</span><span class="token4">'.txt'</span><span class="token1">)</span><span class="token1">;</span><span class="token6">//使用cookie</span> $data <span class="token">=</span> <span class="token3">curl_exec</span><span class="token1">(</span>$ch<span class="token1">)</span><span class="token1">;</span> <span class="token6">//发送请求</span> <span class="token3">curl_close</span><span class="token1">(</span>$ch<span class="token1">)</span><span class="token1">;</span> <span class="token6">//关闭curl</span> <span class="token2">return</span> <span class="token3">json_decode</span><span class="token1">(</span>$data<span class="token1">,</span> <span class="token5">true</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">//返回数据</span> <span class="token6">//return $data;</span> <span class="token1">}</span> <span class="token1">}</span> ``` ```