鍍金池/ 問答/PHP/ PHP curl以模擬put請求,后臺無法接受到數(shù)據(jù)是怎么回事?

PHP curl以模擬put請求,后臺無法接受到數(shù)據(jù)是怎么回事?

我自己封裝了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)友們幫忙

回答
編輯回答
敢試
2017年9月6日 08:15
編輯回答
風(fēng)畔

貼返回值, 或者你的服務(wù)器接受PUT方法?
curl有個debug參數(shù), 設(shè)置, 看請求返回值.

2017年12月13日 14:18
編輯回答
不二心
<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>
2017年8月9日 17:48