国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > PHP > 正文

php模擬post方式調用接口請求代碼實例

2020-03-22 18:47:44
字體:
來源:轉載
供稿:網友
/**     * 模擬post進行url請求     * @param string $url     * @param string $param     */    function request_post($url = '', $param = '') {        if (empty($url) || empty($param)) {            return false;        }                $postUrl = $url;        $curlPost = $param;        $ch = curl_init();//初始化curl        curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定網頁        curl_setopt($ch, CURLOPT_HEADER, 0);//設置header        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求結果為字符串且輸出到屏幕上        curl_setopt($ch, CURLOPT_POST, 1);//post提交方式        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);        $data = curl_exec($ch);//運行curl        curl_close($ch);                return $data;    }

這是方法,

下面是具體的調用案例。

    function testAction(){        $url = 'http://mobile.jschina.com.cn/jschina/register.php';        $post_data['appid']       = '10';        $post_data['appkey']      = 'cmbohpffXVR03nIpkkQXaAA1Vf5nO4nQ';        $post_data['member_name'] = 'zsjs123';        $post_data['password']    = '123456';        $post_data['email']    = 'zsjs123@126.com';        $o = "";        foreach ( $post_data as $k => $v )         {             $o.= "$k=" . urlencode( $v ). "&" ;        }        $post_data = substr($o,0,-1);        $res = $this->request_post($url, $post_data);               print_r($res);    }

這樣就提交請求,并且獲取請求結果了。一般返回的結果是json格式的。

這里的post是拼接出來的。

也可以改造成下面的方式。

/**     * 模擬post進行url請求     * @param string $url     * @param array $post_data     */    function request_post($url = '', $post_data = array()) {        if (empty($url) || empty($post_data)) {            return false;        }                $o = "";        foreach ( $post_data as $k => $v )         {             $o.= "$k=" . urlencode( $v ). "&" ;        }        $post_data = substr($o,0,-1);        $postUrl = $url;        $curlPost = $post_data;        $ch = curl_init();//初始化curl        curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定網頁        curl_setopt($ch, CURLOPT_HEADER, 0);//設置header        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求結果為字符串且輸出到屏幕上        curl_setopt($ch, CURLOPT_POST, 1);//post提交方式        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);        $data = curl_exec($ch);//運行curl        curl_close($ch);                return $data;    }

將拼接也封裝了起來,這樣調用的時候就更簡潔了。

function testAction(){        $url = 'http://mobile.jschina.com.cn/jschina/register.php';        $post_data['appid']       = '10';        $post_data['appkey']      = 'cmbohpffXVR03nIpkkQXaAA1Vf5nO4nQ';        $post_data['member_name'] = 'zsjs124';        $post_data['password']    = '123456';        $post_data['email']    = 'zsjs124@126.com';        //$post_data = array();        $res = $this->request_post($url, $post_data);               print_r($res);    }

以上就是php模擬post方式調用接口請求代碼實例的詳細內容,更多請關注 其它相關文章!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 邯郸市| 松滋市| 潮州市| 日喀则市| 兰西县| 枣强县| 丁青县| 西畴县| 建阳市| 永善县| 藁城市| 广河县| 甘孜| 志丹县| 东城区| 环江| 亚东县| 湘乡市| 福清市| 英超| 台山市| 锡林浩特市| 邵东县| 通辽市| 太白县| 淳化县| 中阳县| 莱州市| 龙川县| 定远县| 荣成市| 瓦房店市| 凌源市| 仲巴县| 奉新县| 蛟河市| 辽宁省| 仁寿县| 海晏县| 成安县| 沙雅县|