最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2020年08月07日 17:01:54   投稿:newname  
這篇文章主要介紹了php模擬post提交請(qǐng)求調(diào)用接口示例解析,文章通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

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)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

民和| 绵阳市| 留坝县| 崇左市| 磐安县| 当涂县| 喀什市| 洛扎县| 金寨县| 汝南县| 巧家县| 浦县| 河池市| 英德市| 古田县| 亚东县| 通山县| 兴仁县| 滁州市| 即墨市| 多伦县| 荥经县| 黑水县| 龙泉市| 迭部县| 盐池县| 平度市| 青河县| 政和县| 芜湖市| 永川市| 安泽县| 上思县| 四平市| 青神县| 乌海市| 林芝县| 长垣县| 咸阳市| 项城市| 辛集市|