欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

curl发送带请求头,带请求参数,带cookie的请求

程序员文章站 2022-05-07 15:13:02
...
/**
     * 发送一个带 cookie 的请求
     *
     * @param string $url
     * @param string $cookie
     * @return mixed
     */
    private function get_result($url, $cookie)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);

        $header = array();
        //curl_setopt($ch,CURLOPT_POST,true);
        //curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_COOKIE, $cookie);

        $content = curl_exec($ch);
        curl_close($ch);
        return $content;
    }

注意请求头的写法:array('Authorization:'.$sign);

/**
     * 发送一个post请求,包含请求头,请求参数
     * @param $url
     * @param $sign
     * @param $jsonStr
     * @return mixed
     */
    private function post($url,$sign,$jsonStr)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);

        $header = array('Authorization:'.$sign);
        curl_setopt($ch,CURLOPT_POST,true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_POSTFIELDS , $jsonStr );
        $content = curl_exec($ch);
        curl_close($ch);
        return $content;
    }
相关标签: curl