php模擬post提交請求調(diào)用接口示例解析
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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- PHP基于curl模擬post提交json數(shù)據(jù)示例
- php使用CURL模擬GET與POST向微信接口提交及獲取數(shù)據(jù)的方法
- PHP socket 模擬POST 請求實(shí)例代碼
- php模擬post上傳圖片實(shí)現(xiàn)代碼
- PHP模擬post提交數(shù)據(jù)方法匯總
- 利用PHP fsockopen 模擬POST/GET傳送數(shù)據(jù)的方法
- php實(shí)現(xiàn)模擬post請求用法實(shí)例
- php模擬post提交數(shù)據(jù)的方法
- php curl模擬post提交數(shù)據(jù)示例
- php curl模擬post請求小實(shí)例
相關(guān)文章
記錄PHP錯誤日志 display_errors與log_errors的區(qū)別
錯誤回顯,一般常用語開發(fā)模式,但是很多應(yīng)用在正式環(huán)境中也忘記了關(guān)閉此選項。錯誤回顯可以暴露出非常多的敏感信息,為攻擊者下一步攻擊提供便利。推薦關(guān)閉此選項2012-10-10
PHP數(shù)組無限分級數(shù)據(jù)的層級化處理代碼
在很多朋友寫無限級分類數(shù)據(jù)時都直接使用遞歸來操作,下面我來介紹一下關(guān)于PHP無限分級代碼優(yōu)化方法,有需要的朋友可參考一下2012-12-12
關(guān)于php程序報date()警告的處理(date_default_timezone_set)
PHP Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function2013-10-10
php中&&和||邏輯運(yùn)算符的高級簡寫(縮寫條件)用法由淺入深講解
php中if進(jìn)行多條件判斷時,使用邏輯運(yùn)算符&&和||(and和or),這樣的寫法很常見也很熟悉。&&和||還有高級簡寫(縮寫條件)用法,比如單獨(dú)一行中“條件A||條件B”進(jìn)行了什么操作?本文將由淺入深詳細(xì)講解php中&&和||邏輯運(yùn)算符的高級簡寫(縮寫條件)用法。2022-11-11
php中用socket模擬http中post或者get提交數(shù)據(jù)的示例代碼
以下是對php中用socket模擬http中post或者get提交數(shù)據(jù)的示例代碼進(jìn)行了介紹,需要的朋友可以過來參考下2013-08-08
全面解析PHP驗(yàn)證碼的實(shí)現(xiàn)原理 附php驗(yàn)證碼小案例
這篇文章主要為大家全面解析了PHP驗(yàn)證碼的實(shí)現(xiàn)原理,附php驗(yàn)證碼小案例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-08-08

