读取 OpenCart 上的 HttpRequest post 数据

Reading a HttpRequest post data on OpenCart

我正在从 C# windowsform 应用程序向 OpenCart 商店发送 HTTP 请求。

这是 fiddler 显示的我正在发送的内容,看起来是正确的。

这是我正在调用的 OpenCart 项目中的代码。

class ControllerEAOpenCartProducts extends ControllerEAOpenCart {

   public function createProduct()
    {
        print_r($this->request);
    }
}

print_r 正在输出以下数据,这些数据似乎不包含我的任何 JSON 数据。

requst的content type好像也不对,不应该像fiddler里的request那样application/json吗?

Content-Type: text/html

HTTP/1.1 200 OK
Date: Wed, 04 Mar 2015 10:50:33 GMT
Server: Apache
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Set-Cookie: PHPSESSID=23dbd33ede35cbeb5486054d06a773a2; path=/; HttpOnly
Set-Cookie: language=en; expires=Fri, 03-Apr-2015 10:50:33 GMT; path=/; domain=opencart.exact3ex.co.uk
Set-Cookie: currency=USD; expires=Fri, 03-Apr-2015 10:50:33 GMT; path=/; domain=opencart.exact3ex.co.uk
Set-Cookie: DYNSRV=lin170; path=/
Connection: close
Content-Type: text/html
Content-Length: 1826

Request Object
(
    [get] => Array
        (
            [route] => EAOpenCart/Products/createProduct
        )

    [post] => Array
        (
        )

    [cookie] => Array
        (
        )

    [files] => Array
        (
        )

    [server] => Array
        (
            [CONTENT_LENGTH] => 200
            [CONTENT_TYPE] => application/json
            [DOCUMENT_ROOT] => /var/sites/o/opencart.exact3ex.co.uk/public_html
            [GATEWAY_INTERFACE] => CGI/1.1
            [HTTP_CONNECTION] => close
            [HTTP_EXPECT] => 100-continue
            [HTTP_HOST] => opencart.exact3ex.co.uk
            [HTTP_X_FORWARDED_FOR] => 194.143.179.2
            [PATH] => /bin
            [QUERY_STRING] => route=EAOpenCart/Products/createProduct
            [REDIRECT_STATUS] => 200
            [REMOTE_ADDR] => 194.143.179.2
            [REMOTE_PORT] => 60376
            [REQUEST_METHOD] => POST
            [REQUEST_URI] => /?route=EAOpenCart/Products/createProduct
            [SCRIPT_FILENAME] => /var/sites/o/opencart.exact3ex.co.uk/public_html/index.php
            [SCRIPT_NAME] => /index.php
            [SERVER_ADDR] => 10.168.1.170
            [SERVER_ADMIN] => you@example.com
            [SERVER_NAME] => opencart.exact3ex.co.uk
            [SERVER_PORT] => 80
            [SERVER_PROTOCOL] => HTTP/1.1
            [SERVER_SIGNATURE] => 
            [SERVER_SOFTWARE] => Apache
            [PHP_SELF] => /index.php
            [REQUEST_TIME_FLOAT] => 1425466233.43
            [REQUEST_TIME] => 1425466233
            [argv] => Array
                (
                    [0] => route=EAOpenCart/Products/createProduct
                )

            [argc] => 1
            [HTTPS] => 
        )

    [request] => Array
        (
            [route] => EAOpenCart/Products/createProduct
        )

)

看起来 opencart 的请求 class 包装了 php 的全局变量。这些仅在提交常规表单数据时才会填充。

要访问json,您需要使用较低级别的输入流:

public function createProduct()
{
    print_r(json_decode(file_get_contents('php://input'), true));
}