婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > php模擬post提交請求調用接口示例解析

php模擬post提交請求調用接口示例解析

熱門標簽:地圖標注符號樣式有 梧州市機器人外呼系統怎么樣 臨沂crm外呼系統平臺 公司外呼系統中心 天客通地圖標注 電子地圖標注怎么修改 如何在世界地圖標注 廈門400電話辦理選易號網 菏澤語音外呼系統運營商

php模擬post提交請求,調用接口

/**
 * 模擬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提交請求調用接口示例解析的文章就介紹到這了,更多相關php模擬post提交請求調用接口內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • PHP基于curl模擬post提交json數據示例
  • php使用CURL模擬GET與POST向微信接口提交及獲取數據的方法
  • PHP socket 模擬POST 請求實例代碼
  • php模擬post上傳圖片實現代碼
  • PHP模擬post提交數據方法匯總
  • 利用PHP fsockopen 模擬POST/GET傳送數據的方法
  • php實現模擬post請求用法實例
  • php模擬post提交數據的方法
  • php curl模擬post提交數據示例
  • php curl模擬post請求小實例

標簽:雞西 迪慶 綿陽 郴州 白城 黃石 貴陽 瀘州

巨人網絡通訊聲明:本文標題《php模擬post提交請求調用接口示例解析》,本文關鍵詞  php,模擬,post,提交,請求,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《php模擬post提交請求調用接口示例解析》相關的同類信息!
  • 本頁收集關于php模擬post提交請求調用接口示例解析的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 溆浦县| 霍州市| 乐都县| 广东省| 安阳县| 高唐县| 武陟县| 嘉禾县| 镇坪县| 巫山县| 汉源县| 兴化市| 金门县| 荆门市| 钦州市| 天柱县| 抚远县| 霞浦县| 巴南区| 河北省| 吴江市| 股票| 泊头市| 北海市| 广东省| 江口县| 大港区| 禄丰县| 石棉县| 中方县| 通化县| 习水县| 华宁县| 汉沽区| 大英县| 罗定市| 罗江县| 印江| 兰州市| 宣城市| 延安市|