zoukankan      html  css  js  c++  java
  • php CURL 发送http请求 GET POST

    * CURL

      http://www.php.net/manual/en/book.curl.php

      http://jp2.php.net/manual/en/function.curl-setopt.php

      GET:

      

    <?php
    /**
     * Created by PhpStorm.
     * User: Mch
     * Date: 7/8/18
     * Time: 16:02
     */
    $ch = curl_init();
    
    $url = 'http://www.tfjyzx.com/news/listTeacherByArea';
    $params = [
        'area'  => '开封市',
        'limit' => 6,
        'type'  => '学生'
    ];
    
    function get_url($url, $params) {
        $a = [];
        foreach ($params as $name => $value) {
            $a[] = $name .'=' .urlencode($value);
        }
        $url .= '?'.implode('&', $a);
        return $url;
    }
    
    $url = get_url($url, $params);
    echo $url.PHP_EOL;
    
    curl_setopt_array($ch, [
        CURLOPT_URL => $url,
        CURLOPT_HEADER => 1,
        CURLOPT_RETURNTRANSFER => 1
    ]);
    
    $data = curl_exec($ch);
    curl_close($ch);
    
    echo $data.PHP_EOL;
    

      

    http://www.tfjyzx.com/news/listTeacherByArea?area=%E5%BC%80%E5%B0%81%E5%B8%82&limit=6&type=%E5%AD%A6%E7%94%9F
    HTTP/1.1 200 OK
    Server: Apache-Coyote/1.1
    Pragma: no-cache
    Cache-Control: no-cache, no-store, max-age=0
    Expires: Thu, 01 Jan 1970 00:00:00 GMT
    Content-Type: application/json;charset=UTF-8
    Content-Language: zh
    Transfer-Encoding: chunked
    Date: Sun, 08 Jul 2018 09:30:23 GMT

    {"teacherList":[{"id":43,"name":"杜姝臻","url":null,"img":"./kaifeng33_img/studentView/4.jpg","school":"开封市33中","datetime":null,"intro":"三十三中优秀学生代表发言","content":null},{"id":42,"name":"朱彤","url":null,"img":"./kaifeng33_img/studentView/3.jpg","school":"开封市33中","datetime":null,"intro":"初二七班","content":null},{"id":41,"name":"张梦岩","url":null,"img":"./kaifeng33_img/studentView/2.jpg","school":"开封市33中","datetime":null,"intro":"三六班","content":null},{"id":40,"name":"周梦寒","url":null,"img":"./kaifeng33_img/studentView/1.jpg","school":"开封市33中","datetime":null,"intro":"三六班","content":null},{"id":20,"name":"程园林","url":null,"img":"./publish/students/kaifeng5/04.jpg","school":"开封市五中","datetime":null,"intro":"15届高三四","content":null},{"id":19,"name":"朱崇","url":null,"img":"./publish/students/kaifeng5/03.jpg","school":"开封市五中","datetime":null,"intro":"15届高三四 朱崇","content":null}]}

    POST:

    <?php
    /**
     * Created by PhpStorm.
     * User: Mch
     * Date: 7/8/18
     * Time: 16:19
     */
    $ch = curl_init();
    
    $s = "POST /student/login HTTP/1.1
    Host: www.tfjyzx.com
    Connection: keep-alive
    Content-Length: 48
    Pragma: no-cache
    Cache-Control: no-cache
    Accept: application/json, text/javascript; q=0.01
    Origin: http://www.tfjyzx.com
    X-Requested-With: XMLHttpRequest
    User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36
    Content-Type: application/x-www-form-urlencoded; charset=UTF-8
    Referer: http://www.tfjyzx.com/login.jsp
    Accept-Encoding: gzip, deflate
    Accept-Language: en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7,ja;q=0.6
    Cookie: experience=show; JSESSIONID=C63BF22874A3B791F212CFCBAFFAE432";
    $header = explode("
    ", $s);
    
    // http://jp2.php.net/manual/en/function.curl-setopt.php
    curl_setopt_array($ch, [
        CURLOPT_URL => 'http://118.190.150.189/student/login',
        // TRUE to include the header in the output.
        CURLOPT_HEADER => 1,
        CURLOPT_RETURNTRANSFER => 1,
        // TRUE to do a regular HTTP POST. This POST is the normal
        // application/x-www-form-urlencoded kind, most commonly used by HTML forms.
        CURLOPT_POST => 1,
        CURLOPT_BINARYTRANSFER => 1,
        CURLOPT_SAFE_UPLOAD => 1,
        CURLOPT_HTTPHEADER => $header,
        CURLOPT_POSTFIELDS => 'username=XXXXX150835&pwd=123456&captcha=&count=1',  // phone number
        /*
        CURLOPT_POSTFIELDS => [
            'username' => 'gmy12345',
            'pwd'      => '123456',
            'captcha'  => '',
            'count'    => 1
        ],
        CURLOPT_ENCODING => 'gzip, deflate',
        CURLOPT_COOKIE => 'experience=show; JSESSIONID=C63BF22874A3B791F212CFCBAFFAE432',
        */
        CURLOPT_TIMEOUT => 5
    ]);
    
    $data = curl_exec($ch);
    curl_close($ch);
    
    echo $data . PHP_EOL;
    

      

    HTTP/1.1 200 OK
    Server: Apache-Coyote/1.1
    Set-Cookie: JSESSIONID=AC471F40CDC460D6D7C14BAACAE21ED5; Path=/; HttpOnly
    Content-Type: application/json;charset=UTF-8
    Content-Length: 118
    Date: Sun, 08 Jul 2018 09:32:30 GMT

    {"result":1,"cause":"登录成功!","school":"濮阳市第八中学","sessionId":"AC471F40CDC460D6D7C14BAACAE21ED5"}

    Sample Request Header:

    Accept:*/*
    Accept-Encoding:gzip, deflate, sdch
    Accept-Language:zh-CN,zh;q=0.8,ja;q=0.6,en;q=0.4,ko;q=0.2,en-US;q=0.2,vi;q=0.2,fr;q=0.2,la;q=0.2
    Connection:keep-alive
    Host:192.168.10.137:9999
    Origin:http://zhanghum:8088
    Referer:http://zhanghum:8088/admin/index.html
    User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36

    Sample Response header:

    Access-Control-Allow-Credentials:true
    Access-Control-Allow-Origin:http://zhanghum:8088
    Content-Encoding:gzip
    Content-Type:application/json
    Date:Mon, 16 Jul 2018 02:03:12 GMT
    Transfer-Encoding:chunked
    Vary:Accept-Encoding

  • 相关阅读:
    智能电视可以安装软件就可以摆脱很多限制,而且可以和PC共享影音资源这个很靠谱。
    【转载】福昕PDF电子文档处理套件 企业版 注册码 注册方法
    OS X系列文章 AirPlay+Apple TV影音方案研究[转]
    SHARP 316L打印机64位驱动问题
    我和电脑的二三事
    北信源DeviceRegister.exe的卸载方法 【转】
    上篇随笔的补充。
    ApplicationCommands用于表示应用程序程序员经常遇到的常见命令,类似于ctrl+c
    WPF中类似使用tab键功能,可以向上向下定位
    c#通过datatable导出excel和word
  • 原文地址:https://www.cnblogs.com/mingzhanghui/p/9280705.html
Copyright © 2011-2022 走看看