【问题标题】:Reading a HttpRequest post data on OpenCart在 OpenCart 上读取 HttpRequest 发布数据
【发布时间】:2015-03-04 19:11:49
【问题描述】:

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

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

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

class ControllerEAOpenCartProducts extends ControllerEAOpenCart {

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

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

requst的内容类型也好像不对,不应该像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
        )

)

【问题讨论】:

    标签: php opencart


    【解决方案1】:

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

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

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

    【讨论】:

    • 完美运行!非常感谢,史蒂夫。
    • @Eritey 没问题,很高兴我能帮上忙
    • @Eritey 查看问题中请求对象的服务器属性,所有标头信息都应该在其中。如果使用 apache 服务器的另一个选项,您可以使用:php.net/manual/en/function.getallheaders.php
    • @Eritey 另外,如果有机会,请将此答案标记为正确
    • 我必须等待时间过去才能将其标记为正确,我现在已经标记了:) 再次感谢您
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多