php curl模擬post請求和提交多維數(shù)組的示例代碼
下面一段代碼給大家介紹php curl模擬post請求的示例代碼,具體代碼如下:
<?php $uri = "http://www.cnblogs.com/test.php";//這里換成自己的服務(wù)器的地址 // 參數(shù)數(shù)組 $data = array ( 'name' => 'tanteng' // 'password' => 'password' ); $ch = curl_init (); // print_r($ch); curl_setopt ( $ch, CURLOPT_URL, $uri ); curl_setopt ( $ch, CURLOPT_POST, 1 ); curl_setopt ( $ch, CURLOPT_HEADER, 0 ); curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data ); $return = curl_exec ( $ch ); curl_close ( $ch ); print_r($return);
2,遠(yuǎn)程服務(wù)器:
<?php if(isset($_POST['name'])){ if(!empty($_POST['name'])){ echo '您好,',$_POST['name'].'!'; } }
下面給大家介紹php中curl模擬post提交多維數(shù)組。
今天需要用curl模擬post提交參數(shù),請求同事提供的一個接口;但是傳遞的參數(shù)中,有一個參數(shù)的值為數(shù)組,用普通的curl post代碼提交,會報錯誤
PHP Notice: Array to string conversion in /test/functions.php on line 30
Notice: Array to string conversion in /test/functions.php on line 30
代碼如下:
<?php $param = array( 'uid' => 123, 'uids' => array(12,455), 'msgType' => 'WITH', 'nick' => 'aaa', ); $url = "http://cx.com/t.php"; //通過curl的post方式發(fā)送接口請求 SendDataByCurl($url,$param); //通過curl模擬post的請求; function SendDataByCurl($url,$data=array()){ //對空格進(jìn)行轉(zhuǎn)義 $url = str_replace(' ','+',$url); $ch = curl_init(); //設(shè)置選項,包括URL curl_setopt($ch, CURLOPT_URL, "$url"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch,CURLOPT_TIMEOUT,3); //定義超時3秒鐘 // POST數(shù)據(jù) curl_setopt($ch, CURLOPT_POST, 1); // 把post的變量加上 curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //執(zhí)行并獲取url地址的內(nèi)容 $output = curl_exec($ch); //釋放curl句柄 curl_close($ch); return $output; }
經(jīng)過修改上面代碼,可以完成提交數(shù)組的功能,而不會報php notice,代碼如下:
//通過curl模擬post的請求; function SendDataByCurl($url,$data=array()){ //對空格進(jìn)行轉(zhuǎn)義 $url = str_replace(' ','+',$url); $ch = curl_init(); //設(shè)置選項,包括URL curl_setopt($ch, CURLOPT_URL, "$url"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch,CURLOPT_TIMEOUT,3); //定義超時3秒鐘 // POST數(shù)據(jù) curl_setopt($ch, CURLOPT_POST, 1); // 把post的變量加上 curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); //所需傳的數(shù)組用http_bulid_query()函數(shù)處理一下,就ok了 //執(zhí)行并獲取url地址的內(nèi)容 $output = curl_exec($ch); $errorCode = curl_errno($ch); //釋放curl句柄 curl_close($ch); if(0 !== $errorCode) { return false; } return $output; }
- php curl批處理實(shí)現(xiàn)可控并發(fā)異步操作示例
- PHP curl 并發(fā)最佳實(shí)踐代碼分享
- php使用curl并發(fā)減少后端訪問時間的方法分析
- php cURL和Rolling cURL并發(fā)方式比較
- PHP使用curl_multi實(shí)現(xiàn)并發(fā)請求的方法示例
- PHP中使用cURL實(shí)現(xiàn)Get和Post請求的方法
- php之curl實(shí)現(xiàn)http與https請求的方法
- php curl請求信息和返回信息設(shè)置代碼實(shí)例
- php curl 獲取https請求的2種方法
- PHP curl批處理及多請求并發(fā)實(shí)現(xiàn)方法分析
相關(guān)文章
php微信公眾號開發(fā)之關(guān)鍵詞回復(fù)
這篇文章主要為大家詳細(xì)介紹了php微信公眾號開發(fā)之關(guān)鍵詞回復(fù),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-10-10Yii框架學(xué)習(xí)筆記之session與cookie簡單操作示例
這篇文章主要介紹了Yii框架學(xué)習(xí)筆記之session與cookie簡單操作,結(jié)合實(shí)例形式分析了Yii框架session與cookie的定義、設(shè)置、讀寫、刪除等簡單操作技巧,需要的朋友可以參考下2019-04-04PHP網(wǎng)頁游戲?qū)W習(xí)之Xnova(ogame)源碼解讀(十)
這篇文章主要介紹了PHP網(wǎng)頁游戲Xnova(ogame)源碼解讀的建造總覽部分,需要的朋友可以參考下2014-06-06laravel實(shí)現(xiàn)按時間日期進(jìn)行分組統(tǒng)計方法示例
這篇文章主要給大家介紹了關(guān)于laravel如何實(shí)現(xiàn)按時間日期進(jìn)行分組統(tǒng)計的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用laravel具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03深入mysql_fetch_row()與mysql_fetch_array()的區(qū)別詳解
本篇文章是對mysql_fetch_row()與mysql_fetch_array()的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06