久久福利_99r_国产日韩在线视频_直接看av的网站_中文欧美日韩_久久一

您的位置:首頁技術(shù)文章
文章詳情頁

php模擬post提交請求調(diào)用接口示例解析

瀏覽:111日期:2022-09-09 13:15:52

php模擬post提交請求,調(diào)用接口

/** * 模擬post進(jìn)行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);//抓取指定網(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); }

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

這里的post是拼接出來的。

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

/** * 模擬post進(jìn)行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);//抓取指定網(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’] = ’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提交請求調(diào)用接口示例解析的文章就介紹到這了,更多相關(guān)php模擬post提交請求調(diào)用接口內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: PHP
相關(guān)文章:
主站蜘蛛池模板: 亚洲精品久久久久久久久久久 | 在线观看中文字幕 | 91成人小视频 | 一区二区三区在线视频播放 | 久久99精品久久久久久琪琪 | 精品视频一区在线观看 | 一级片在线观看 | 9l蝌蚪porny中文自拍 | 国产依人 | 一区免费看 | 欧美色视频在线观看 | 亚洲免费人成在线视频观看 | 免费黄色小视频 | 91久久精品国产 | 91网址| av网址在线播放 | 亚洲视频在线看 | 国产精品永久免费 | 中文字幕日韩一区二区三区 | 激情欧美一区二区三区中文字幕 | 欧美成人免费在线视频 | 亚洲日韩欧美一区二区在线 | 亚洲精品wwww | 麻豆国产一区二区三区四区 | 国产精品视频999 | www.国产欧美 | 久久亚洲一区二区三区四区 | 四虎影院免费网址 | 久久久久久久久99精品 | 免费看国产片在线观看 | 天天插天天操天天干 | 日本国产一区二区 | 久国产 | 久久综合狠狠综合久久综合88 | 一级特黄aaa大片在线观看 | 国产真实乱全部视频 | 日韩视频在线观看一区 | 中文字幕国产区 | 美女操网站 | 伊人99 | 国产精品久久久久久久久久久免费看 |