Home >Backend Development >PHP Tutorial >Examples of two methods for php to access url (get and post code)

Examples of two methods for php to access url (get and post code)

不言
不言Original
2018-09-12 16:53:095741browse

This article brings you examples (code) of two methods of accessing URLs in PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

get request

/*
     *  php访问url路径,get请求
     */
    function curl_file_get_contents($durl){
        // header传送格式
        $headers = array(
            "token:1111111111111",
            "over_time:22222222222",
        );
        // 初始化
        $curl = curl_init();
        // 设置url路径
        curl_setopt($curl, CURLOPT_URL, $durl);
        // 将 curl_exec()获取的信息以文件流的形式返回,而不是直接输出。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true) ;
        // 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回
        curl_setopt($curl, CURLOPT_BINARYTRANSFER, true) ;
        // 添加头信息
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        // CURLINFO_HEADER_OUT选项可以拿到请求头信息
        curl_setopt($curl, CURLINFO_HEADER_OUT, true);
        // 执行
        $data = curl_exec($curl);
        // 打印请求头信息
//        echo curl_getinfo($curl, CURLINFO_HEADER_OUT);
        // 关闭连接
        curl_close($curl);
        // 返回数据
        return $data;
    }

post request

/*
     *   php访问url路径,post请求
     *
     *   durl   路径url
     *   post_data   array()   post参数数据
     */
    public function curl_file_post_contents($durl, $post_data){
        // header传送格式
        $headers = array(
            "token:1111111111111",
            "over_time:22222222222",
        );
        //初始化
        $curl = curl_init();
        //设置抓取的url
        curl_setopt($curl, CURLOPT_URL, $durl);
        //设置头文件的信息作为数据流输出
        curl_setopt($curl, CURLOPT_HEADER, false);
        //设置获取的信息以文件流的形式返回,而不是直接输出。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        //设置post方式提交
        curl_setopt($curl, CURLOPT_POST, true);
        // 设置post请求参数
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
        // CURLINFO_HEADER_OUT选项可以拿到请求头信息
        curl_setopt($curl, CURLINFO_HEADER_OUT, true);
        //执行命令
        $data = curl_exec($curl);
        // 打印请求头信息
//        echo curl_getinfo($curl, CURLINFO_HEADER_OUT);
        //关闭URL请求
        curl_close($curl);
        //显示获得的数据
        return $data;
    }

Note: The return result can be parsed through json_decode after the call:

For example: $data= $this- >curl_file_post_contents($dataurl, $post_data);

$dataarr = json_decode($data, true);

Related recommendations:

PHP : 6 GET and POST request sending methods, 6 get

The difference between ASP.NET Get and Post submissions:

The above is the detailed content of Examples of two methods for php to access url (get and post code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn