zoukankan      html  css  js  c++  java
  • CURL 模拟http提交

    1:CURL模拟get提交

    private function httpGet($url) {
       $curl = curl_init();
       curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
       curl_setopt($curl, CURLOPT_TIMEOUT, 500);
       curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
       curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
       curl_setopt($curl, CURLOPT_URL, $url);
    
       $res = curl_exec($curl);
       curl_close($curl);
    
       return $res;
    }

    2:CURL模拟post提交

    //模拟post提交
    function http_request($url , $data , $method="POST"){
    $ch = curl_init(); //初始化
    curl_setopt($ch , CURLOPT_URL , $url); //请求地址
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); //请求方式
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; windows NT 5.0)');
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
    if($method == 'POST'){
    //POST方式的时候添加数据
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $tmpinfo = curl_exec($ch); //执行
    if(curl_errno($ch)){
    return curl_error($ch);
    }
    curl_close($ch); //关闭
    return $tmpinfo;
    }
    
    curl模拟post提交
    function https_request($url, $data = null){
         $curl = curl_init();
         curl_setopt($curl, CURLOPT_URL, $url);
         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
         curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
         if (!empty($data)){
              curl_setopt($curl, CURLOPT_POST, 1);
              curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
         }
         curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
         $output = curl_exec($curl);
         curl_close($curl);
         return $output;
    }
  • 相关阅读:
    5.4.3 果园里的树
    5.3.2 字母重排
    5.3.1 6174问题
    Codeforces Round #253 (Div. 2), problem: (B)【字符串匹配】
    POJ1054 枚举【STL__binary_search()_的应用】
    432B
    0x3f3f3f3f...编程中无穷大常量的设置技巧
    2014年百度之星程序设计大赛 资格赛第一题 (longlong)
    Tarjan
    Dijkstra、Dij + heap、Floyd、SPFA、 SPFA + SLF Template
  • 原文地址:https://www.cnblogs.com/jing1208/p/6252604.html
Copyright © 2011-2022 走看看