PHP POST请求返回404状态代码

时间:2019-09-01 07:55:16

标签: php http post request

我正在发出一个发布请求,该请求成功时将返回200状态代码,并且当我使用邮递员尝试时可以正常工作。

当我尝试使用POST发出PHP请求时,请求成功,但是状态代码为404

这是我的代码。

            $url = 'my_url';
            $data = array('key1' => 'value1', 'key2' => 
            'value2');
            $options = array(
                'http' => array(
                    'method'  => 'POST',
                    'content' => json_encode( $data ),
                    'header'=>  "Content-Type: application/json\r\n",
                    'ignore_errors' => true
                )
            );
            $context  = stream_context_create( $options );
            $result = file_get_contents( $url, false, $context );
            $response = json_decode( $result );
            $headers = get_headers($url);
            Helper::pre_die($headers);
            $code = substr($headers[0], 9, 3);

这是我得到的标题:

    Array
    (
        [0] => HTTP/1.1 404 Not Found
        [1] => Server: nginx/1.14.0 (Ubuntu)
        [2] => Date: Sun, 01 Sep 2019 07:52:34 GMT
        [3] => Content-Type: text/html; charset=utf-8
        [4] => Content-Length: 146
        [5] => Connection: close
        [6] => X-Powered-By: Express
        [7] => Vary: Origin, Accept-Encoding
        [8] => Content-Security-Policy: default-src 'self'
        [9] => X-Content-Type-Options: nosniff
    )

任何帮助将不胜感激。

0 个答案:

没有答案