Reading a HttpRequest post data on OpenCart

吃可爱长大的小学妹 提交于 2020-01-06 03:26:07

问题


I am sending a HTTP request from a C# windowsform application to an OpenCart store.

This is what fiddler is showing that I'm sending, which looks correct.

This is the code I have in my OpenCart project that I'm calling.

class ControllerEAOpenCartProducts extends ControllerEAOpenCart {

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

The print_r is outputting the following data which doesn't appear to be containing any of my JSON data.

The content type of the requst also seems to be incorrect, shouldn't it be application/json like the request in fiddler shows?

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
        )

)

回答1:


Looks like opencart's request class wraps php's globals. These only get populated when regular form data is submitted.

To access json, you need to use the lower level input stream:

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


来源:https://stackoverflow.com/questions/28852679/reading-a-httprequest-post-data-on-opencart

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!