简体   繁体   English

使用PUT通过C#HttpWebRequest检索PHP中的数据— C#->使用Slim Rest Framework的PHP

[英]retrieving data in PHP by C# HttpWebRequest using PUT — C# -> PHP using Slim Rest Framework

I´m new to Slim and I´ve some issues retrieving data sending stuff via a httpwebrequest using "PUT". 我是Slim的新手,在使用“ PUT”通过httpwebrequest检索数据发送内容时遇到一些问题。 I know http is not rest! 我知道http不休息! ;) and i know the data is send correctly. ;)并且我知道数据发送正确。 so, what am i missing? 所以,我想念什么? yes, i could send all the data via the headers, but i don´t want to use such a work around. 是的,我可以通过标题发送所有数据,但是我不想使用这种解决方法。

so this is what i do: 所以这就是我要做的:

C#: C#:

var request = WebRequest.Create(url) as HttpWebRequest;
request.ContentType = "application/x-www-form-urlencoded";
request.ProtocolVersion = HttpVersion.Version11;
request.KeepAlive = true;

// Set request method
request.Method = method.ToUpper(); // in this case: "PUT"


var dataByte = Encoding.UTF8.GetBytes("test=test"); // <-- DATA to be send
request.ContentLength = dataByte.Length;

using (Stream dataStream = request.GetRequestStream())
{
    dataStream.Write(dataByte, 0, dataByte.Length);
}


var res = (HttpWebResponse)request.GetResponse();
var resStream = res.GetResponseStream();
if (resStream != null)
{
    var reader = new StreamReader(resStream);
    responseFromServer = reader.ReadToEnd();
    reader.Close();
}
if (resStream != null) resStream.Close();
res.Close();

PHP: using Slim and "PUT" PHP:使用Slim和“ PUT”

$app->put('/someendpoint', function ($somevar) {
   // THIS IST THE PLACE WHERE I NEED THE DATA : "test"
});

works perfectly when using POST or GET (of course), like 在使用POST或GET(当然)时效果很好,例如

if(isset($_POST['test']))
echo $_POST['test'];

but i need something like 但我需要类似的东西

$_PUT['test']

...soooo how could i do that? ...我该怎么办? i´m sure there´sa solution and i think may people out there have issues on that. 我敢肯定有解决方案,我认为外面的人可能对此有疑问。

thx in advance for helping me out! 提前感谢帮助我! :) :)

...found it: i did some research and read this tutorial again (...and again...and again...) ;) ...找到它:我做了一些研究,并再次阅读了本教程(...再一次...再一次...);)

http://www.androidhive.info/2014/01/how-to-create-rest-api-for-android-app-using-php-slim-and-mysql-day-23/ http://www.androidhive.info/2014/01/how-to-create-rest-api-for-android-app-using-php-slim-and-mysql-day-23/

all i had to do is using this neat function to get the body information to parse the stuff: 我要做的就是使用这个简单的函数来获取正文信息以解析这些内容:

function verifyRequiredParams($required_fields) {
    $error = false;
    $error_fields = "";
    $request_params = array();
    $request_params = $_REQUEST;
    // Handling PUT request params
    if ($_SERVER['REQUEST_METHOD'] == 'PUT') {
        $app = \Slim\Slim::getInstance();
        parse_str($app->request()->getBody(), $request_params);
    }
    foreach ($required_fields as $field) {
        if (!isset($request_params[$field]) || strlen(trim($request_params[$field])) <= 0) {
            $error = true;
            $error_fields .= $field . ', ';
        }
    }

    if ($error) {
        // Required field(s) are missing or empty
        // echo error json and stop the app
        $response = array();
        $app = \Slim\Slim::getInstance();
        $response["error"] = true;
        $response["message"] = 'Required field(s) ' . substr($error_fields, 0, -2) . ' is missing or empty';
        echoRespnse(400, $response);
        $app->stop();
    }
}

and i had to change my api call to: 我不得不将我的api调用更改为:

$app->put('/someendpoint', function ($somevar) use($app){

    verifyRequiredParams(array('test'));
    $test= $app->request->put('test');
    echo $test;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM