柳暗花明笔记

    本人总结最新版php发送http头及php取http请求头,无须安装扩展

    所属分类:[ php&web ] 猫哲学家 发表于:2015-11-10 17:16 浏览:522

    客户端

    $url = 'http://loc.img.get.com/get_product_images_v1.php';

    $data = array('haha' => 'haha', '1' => 1);

    // 注意header的key不能有下划线,认真观察header的写法
    $header = array('phpauthuser:123456;phpauthpass:565', 'aaa:8', 'bb'=>355);

    http_request($url, 'get', $header, $data);

    function http_request($url, $m = 'get', $header = array(), $data = NULL)
    {
        try
        {
            $ch = curl_init($url);
            $cookie = 'cookie';

            curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
            curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);

            curl_setopt($ch, CURLOPT_HEADER, TRUE);
            curl_setopt($ch, CURLOPT_TIMEOUT,180);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

            curl_setopt($ch , CURLOPT_COOKIEJAR, $cookie);  // get cookie , then sent next request (TODO)

            // Send cookie
            if ($m == 'post') {
                curl_setopt($ch, CURLOPT_POST, 1); // default is get
                curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // $data array | string
            }

            $response = curl_exec($ch);

            // Get curl error exception
            $curl_error_no = curl_errno($ch);
            if ($curl_error_no > 0)
            {
                die(curl_error($ch));
                // Do request again
                // return self::http_request($url, $m, $header, $data);
            }

            $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
            $response_header = substr($response, 0, $header_size);
            $response_body = substr($response, $header_size);


            print_r(array('header' => $response_header, 'body' => $response_body));
        }
        catch (Exception $e) // Can not get curl_error !!!!
        {
            die($e->getMessage());
        }
    }


    服务端

    print_r($_SERVER);


    print_r(get_http_request_headers());



    function get_http_request_headers()
    {

        $http_request_headers = array();

        foreach ($_SERVER as $key => $value)
        {

            if (substr($key, 0, 5) == 'HTTP_')
            {
                $http_request_headers[str_replace('HTTP_', '', $key)] = $value;
            }
        }

        return $http_request_headers;
    }

    输出效果如下,
      [SERVER_PORT] => 80
        [SERVER_NAME] => loc.img.get.com
        [REDIRECT_STATUS] => 200
        [HTTP_HOST] => loc.img.get.com
        [HTTP_ACCEPT] => */*
        [HTTP_PHPAUTHUSER] => 123456;phpauthpass:565
        [HTTP_AAA] => 8
        [PHP_SELF] => /get_product_images_v1.php
        [REQUEST_TIME_FLOAT] => 1447146417.4239
        [REQUEST_TIME] => 1447146417
    )
    Array
    (
        [HOST] => loc.img.get.com
        [ACCEPT] => */*
        [PHPAUTHUSER] => 123456;phpauthpass:565
        [AAA] => 8
    )

    可以在浏览器下直接编辑header再发送

    标签: php  http 
    上一编文章 女程序员昨晚做了一个梦 下一编文章 linux kill 进程有时候kill不了
    最近文章
    发表评论