zoukankan      html  css  js  c++  java
  • php中使用curl两个例子

    第一个例子:

    调用一个天气预告的接口

    $data = 'theCityName=石家庄';
    $cUrl = curl_init();
    curl_setopt($cUrl, CURLOPT_URL, "http://www.webxml.com.cn/webservices/weatherwebservice.asmx/getWeatherbyCityName");
    curl_setopt($cUrl, CURLOPT_HEADER, 0);
    curl_setopt($cUrl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($cUrl, CURLOPT_POST, 1);
    curl_setopt($cUrl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($cUrl, CURLOPT_POSTFIELDS, $data);
    curl_setopt($cUrl, CURLOPT_HTTPHEADER, array("application/x-www-form-urlencoded; charset=utf-8", "Content-length: ".strlen($data)));
    
    
    $output = curl_exec($cUrl);
    
    if(!curl_errno($cUrl)) {
        echo $output;
    }else{
        echo "Curl error:  ".curl_error($cUrl);
    }
    //echo str_replace("百度", "^_^", $output);
    
    curl_close($cUrl);

    第二个例子:

    模拟登录一个站点并保存某个页面

    $data = 'name=admin&password=123456';
    $cUrl = curl_init();
    curl_setopt($cUrl, CURLOPT_URL, "http://www.***.com/a/login.html");
    curl_setopt($cUrl, CURLOPT_RETURNTRANSFER, 1);
    date_default_timezone_set('PRC');
    
    curl_setopt($cUrl, CURLOPT_COOKIESESSION, TRUE);
    curl_setopt($cUrl, CURLOPT_COOKIEFILE, "cookiefile");
    curl_setopt($cUrl, CURLOPT_COOKIEJAR, "cookiefile");
    curl_setopt($cUrl, CURLOPT_COOKIE, session_name()."=".session_id());
    curl_setopt($cUrl, CURLOPT_HEADER, 0);
    curl_setopt($cUrl, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($cUrl, CURLOPT_POST, 1);
    //curl_setopt($cUrl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($cUrl, CURLOPT_POSTFIELDS, $data);
    curl_setopt($cUrl, CURLOPT_HTTPHEADER, array("application/x-www-form-urlencoded; charset=utf-8", "Content-length: ".strlen($data)));
    curl_exec($cUrl);
    
    curl_setopt($cUrl, CURLOPT_URL, "http://www.***.com/a/analysisIndex.html");//要保存的页面
    curl_setopt($cUrl, CURLOPT_POST, 0);
    curl_setopt($cUrl, CURLOPT_HTTPHEADER, array("Content-type: text/xml"));
    $output = curl_exec($cUrl);
    curl_close($cUrl);
    echo $output;
  • 相关阅读:
    [哈工大操作系统]一、环境配置
    [算法笔记]带权并查集
    C++:Reference to non-static member function must be called
    [算法笔记]并查集
    C++:string.size()比较问题
    [算法笔记]二分总结
    【LeetCode每日一题】2020.10.15 116. 填充每个节点的下一个右侧节点指针
    1Manjaro的安装
    【《你不知道的JS(中卷②)》】一、 异步:现在与未来
    【LeetCode每日一题】2020.7.14 120. 三角形最小路径和
  • 原文地址:https://www.cnblogs.com/Caoxt/p/4645974.html
Copyright © 2011-2022 走看看