php模擬post提交請(qǐng)求調(diào)用接口示例解析
php模擬post提交請(qǐng)求,調(diào)用接口
/** * 模擬post進(jìn)行url請(qǐng)求 * @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);//抓取指定網(wǎng)頁 curl_setopt($ch, CURLOPT_HEADER, 0);//設(shè)置header curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求結(jié)果為字符串且輸出到屏幕上 curl_setopt($ch, CURLOPT_POST, 1);//post提交方式 curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost); $data = curl_exec($ch);//運(yùn)行curl curl_close($ch); return $data; }
這是方法,
下面是具體的調(diào)用案例。
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); }
這樣就提交請(qǐng)求,并且獲取請(qǐng)求結(jié)果了。一般返回的結(jié)果是json格式的。
這里的post是拼接出來的。
也可以改造成下面的方式。
/** * 模擬post進(jìn)行url請(qǐng)求 * @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);//抓取指定網(wǎng)頁 curl_setopt($ch, CURLOPT_HEADER, 0);//設(shè)置header curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求結(jié)果為字符串且輸出到屏幕上 curl_setopt($ch, CURLOPT_POST, 1);//post提交方式 curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost); $data = curl_exec($ch);//運(yùn)行curl curl_close($ch); return $data; }
將拼接也封裝了起來,這樣調(diào)用的時(shí)候就更簡(jiǎn)潔了。
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); }
到此這篇關(guān)于php模擬post提交請(qǐng)求調(diào)用接口示例解析的文章就介紹到這了,更多相關(guān)php模擬post提交請(qǐng)求調(diào)用接口內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- PHP基于curl模擬post提交json數(shù)據(jù)示例
- php使用CURL模擬GET與POST向微信接口提交及獲取數(shù)據(jù)的方法
- PHP socket 模擬POST 請(qǐng)求實(shí)例代碼
- php模擬post上傳圖片實(shí)現(xiàn)代碼
- PHP模擬post提交數(shù)據(jù)方法匯總
- 利用PHP fsockopen 模擬POST/GET傳送數(shù)據(jù)的方法
- php實(shí)現(xiàn)模擬post請(qǐng)求用法實(shí)例
- php模擬post提交數(shù)據(jù)的方法
- php curl模擬post提交數(shù)據(jù)示例
- php curl模擬post請(qǐng)求小實(shí)例
相關(guān)文章
PHP三層結(jié)構(gòu)(下) PHP實(shí)現(xiàn)AOP
讓我們把注意力集中到中間服務(wù)層上來。中間服務(wù)層代碼比較簡(jiǎn)單,只是調(diào)用數(shù)據(jù)訪問層代碼將留言保存到數(shù)據(jù)庫。2010-07-07php簡(jiǎn)單實(shí)現(xiàn)批量上傳圖片的方法
這篇文章主要介紹了php簡(jiǎn)單實(shí)現(xiàn)批量上傳圖片的方法,實(shí)例分析了php文件傳輸?shù)膶?shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-05-05詳談PHP面向?qū)ο笾谐S玫年P(guān)鍵字和魔術(shù)方法
下面小編就為大家?guī)硪黄斦凱HP面向?qū)ο笾谐S玫年P(guān)鍵字和魔術(shù)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02php 模擬 asp.net webFrom 按鈕提交事件的思路及代碼
這篇文章主要介紹了php模擬asp.net webFrom 按鈕提交事件的思路及代碼,有需要的朋友可以參考一下2013-12-12