国产探花免费观看_亚洲丰满少妇自慰呻吟_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方式調用接口請求代碼實例的詳細內容,更多請關注 其它相關文章!

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 南丰县| 吉水县| 广西| 江油市| 鄂伦春自治旗| 辽宁省| 洛扎县| 仪陇县| 馆陶县| 广德县| 蓬安县| 沧州市| 砀山县| 新干县| 芮城县| 晋中市| 左贡县| 浮梁县| 清原| 江津市| 阿拉善盟| 耒阳市| 辽中县| 玉环县| 宜川县| 诸城市| 丹江口市| 兴安盟| 鄂托克旗| 正镶白旗| 吉林市| 滦平县| 吐鲁番市| 靖州| 宁津县| 鹿泉市| 呈贡县| 武清区| 浮梁县| 新绛县| 峨山|