PHP不使用curl擴展而使用內置函數發送請求

通常情況下,php發送請求是通過curl擴展實現的,那麼不通過curl是否可以直接發送請求?php已有了內置的函數來模擬POST/GET請求,即stream_context_create(),直接看demo吧!

    protected function php_ajax()
    {
        $url = "http(s)://www.example.com";
        $post_data= [
            'appkey'=>'',
            'channel'=>'',
            'content'=>''
        ];//自定義內容
        $postdata = http_build_query($post_data);
        $options = array(
            'http' => array(
                'method' => 'POST',
                'header' => 'Content-type:application/x-www-form-urlencoded',
                'content' => $postdata,
                'timeout' => 15 * 60
            )
        );
        $context = stream_context_create($options);
        file_get_contents($url, false, $context);
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章