我自己封裝了curl工具類,測試表現(xiàn):get,post,delete方式后臺都能正確接收到前面?zhèn)鞯膮?shù),但是put方式就是獲取不到參數(shù).
1.相關(guān)代碼:
index.php 入口請求文件
require_once 'MyCurl.class.php';
$data = ['param' => '成功', 'param1' => '這是神馬'];
$res = MyCurl::send('http://localhost/servername/admin/test/ceshi', $data, 'put');
MyCurl.class.php curl工具類文件
class MyCurl
{
private static $url = ''; //請求url
private static $method = 'get'; //請求方式
private static $oriUrl = ''; //形式如 http://localhost
private static $data = []; //請求參數(shù)
public static function send($url, $data = [], $method = 'get')
{
$url or die('url can\'t be null');
self::$url = $url;
self::$method = strtoupper($method);
$urlArr = parse_url($url);
self::$oriUrl = $urlArr['scheme'] . '://' . $urlArr['host']; //形式為 http://localhost
self::$data = $data;
in_array(strtoupper(self::$method), array('GET', 'POST', 'PUT', 'DELETE')) or exit('error request method type!');
return self::doRequest();
}
/**
* 基礎(chǔ)發(fā)起curl請求函數(shù)
* @return boolean
*/
private static function doRequest()
{
$ch = curl_init(); //初始化curl
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //設(shè)置超時限制防止死循環(huán)
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); //設(shè)置發(fā)起連接前的等待時間,如果設(shè)置為0,則無限等待。
curl_setopt($ch, CURLOPT_URL, self::$url);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override:' . self::$method));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //為1:curl_exec()有返回值,為0:curl_exec()無返回值,直接輸出.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//4)"User-Agent: "頭的字符串。
curl_setopt($ch, CURLOPT_USERAGENT, 'SSTS Browser/1.0');
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)'); // 模擬用戶使用的瀏覽器
switch (self::$method) {
case 'GET':
break;
case 'POST':
curl_setopt($ch, CURLOPT_POST, true); //POST方式
break;
case 'PUT':
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); //PUT方式
break;
case 'DELETE':
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); //DELETE方式
break;
default:
die('error :no method type');
break;
}
if (self::$data) {
if (self::$method == 'GET') {
curl_setopt($ch, CURLOPT_URL, self::$url . '?' . http_build_query(self::$data));
} else {
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(self::$data));
}
} else {
self::$method != 'GET' && die('POST/PUT/DELETE請求需要參數(shù)');
}
$data = curl_exec($ch); //運行curl
if (!$data) {
echo curl_error($ch);
}
curl_close($ch);
return $data;
}
}
處理請求文件,基于tp3.2
<?php
/**
* 測試類
*/
namespace Admin\Controller;
use Think\Controller;
class TestController extends Controller\RestController
{
public function ceshi()
{
$param = I('param.param');
echo '請求方法:'.$_SERVER['REQUEST_METHOD'];
echo '請求方法:'.$this->_method.'<br/>';
echo '請求參數(shù):';
echo $param;
print_r($_REQUEST);
parse_str(file_get_contents('php://input'), $data);
print_r($data);
$test= file_get_contents('php://input');
print_r($test);
}
}
無論怎么做都接收不了put請求方式傳送過來的參數(shù),讓我很納悶,在這上面也糾結(jié)很久了.想請fault的網(wǎng)友們幫忙
<div class="manual-right" style="top: 0">
<div class="m-article">
<div class="article-head">
<h1 id="title">
{:cookie('a_title')}
</h1>
</div>
<div class="article-wrap">
<div class="article-view">
<div class="view-body think-editor-content">
{:cookie('a_content')}
</div>
</div>
</div>
<div class="think-loading loading-ripple"><span class="loading-inner"><i class="loading-image"></i><b class="loading-text"></b></span></div>
</div>
</div>
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
北大青鳥中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國一站式人才培養(yǎng)平臺、一站式人才輸送平臺。2014年4月3日在美國成功上市,融資1
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項目經(jīng)理從事移動互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺面向?qū)ο箝_發(fā)經(jīng)驗,技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點難點突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。