久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

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

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

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

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)頁(yè) 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是拼接出來(lái)的。

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

/** * 模擬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)頁(yè) 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; }

將拼接也封裝了起來(lái),這樣調(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)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: PHP
相關(guān)文章:
主站蜘蛛池模板: 克山县| 双辽市| 维西| 丰县| 利川市| 射阳县| 小金县| 噶尔县| 龙南县| 德保县| 舟曲县| 石泉县| 博罗县| 靖西县| 枣庄市| 门头沟区| 原平市| 凌海市| 四平市| 北碚区| 定陶县| 瑞安市| 桑日县| 夏津县| 肃宁县| 凤翔县| 中牟县| 泽普县| 浙江省| 英德市| 衡水市| 大新县| 凤山县| 秦安县| 宁晋县| 怀柔区| 苏州市| 阜平县| 昔阳县| 临城县| 潼关县|