WordPress使用wp_remote_get和wp_remote_post进行get和post请求,并封装为函数

今天小薛给大家带来有关于WordPress使用wp_remote_get和wp_remote_post进行get和post请求,并封装为函数的具体操作方法,通常PHP是使用CURL请求post和get请求,在WordPress中,我们可以使用wp_remote_get和wp_remote_post进行get和post请求,这里讲自己封装的get方法和post方法分享出来,便于后续使用。

/**
     * @author 一只薛眠羊 <www.xuemy.cn>
     * @param $url string 请求地址
     * @param $args array 请求参数
     */
    public static function doGet($url, $args = array())
    {
        $args = wp_parse_args($args, array(
            'body'                => array(),
            'timeout'            => 5, //放弃之前的等待时间
            'method'            => 'GET',
            'sslverify'            => false,
            'redirection'            => 5,   //跳转之前的等待时间
            'blocking'            => true,
            'headers' => array(),
            'cookies' => array(),
        ));
        try {
            $response = wp_remote_get($url, $args);
            $body = wp_remote_retrieve_body($response);
            $body_json_decoded = json_decode($body, true);
            if (is_null($body_json_decoded)) {
                $body = wp_remote_retrieve_body($response);
            } else {
                $body = $body_json_decoded;
            }
            $http_code = wp_remote_retrieve_response_code($response);
        } catch (\Exception $e) {
            echo array('error' => 1, 'msg' => $e->getMessage());
            exit();
        }
        $data = array(
            'code' => $http_code,
            'body' => $body
        );
        return json_encode($data);
    }

    /**
     * @author 一只薛眠羊 <www.xuemy.cn>
     * @param $url string 请求地址
     * @param $args array 请求参数
     */
    public static function doPost($url, $args = array())
    {
        $args = wp_parse_args($args, array(
            'body'                => array(),
            'timeout'            => 5, //放弃之前的等待时间
            'method'            => 'POST',
            'sslverify'            => false,
            'redirection'            => 5,   //跳转之前的等待时间
            'blocking'            => true,
            'headers' => array(),
            'cookies' => array(),
        ));
        try {
            $response = wp_remote_post($url, $args);
            $body = wp_remote_retrieve_body($response);
            $body_json_decoded = json_decode($body, true);
            if (is_null($body_json_decoded)) {
                $body = wp_remote_retrieve_body($response);
            } else {
                $body = $body_json_decoded;
            }
            $http_code = wp_remote_retrieve_response_code($response);
        } catch (\Exception $e) {
            echo array('error' => 1, 'msg' => $e->getMessage());
            exit();
        }

        $data = array(
            'code' => $http_code,
            'body' => $body
        );
        return json_encode($data);
    }

请求示例:

// get请求
$hitokoto_response = doGet('https://v1.hitokoto.cn/?c=f&encode=text');

//post请求
        $apiUrl = 'https://tenapi.cn/v2/weather';
        $data = doPost($apiUrl,[
            'body' =>['city' => $city],
        ]);
© 版权声明
THE END
喜欢就支持一下吧
点赞14赞赏 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容