Hướng dẫn php json = file_get_contents - php json = file_get_contents

Tôi đang cố gắng đọc trong một tin nhắn JSON trong ứng dụng PHP của mình và đây là mã PHP của tôi:

$json = file_get_contents('php://input');
$obj = json_decode($json, TRUE);
echo $obj->{'S3URL'};

Khi tôi làm điều này, tôi đang gặp lỗi sau:

Trying to get property of non-object in setImage.php on line 25 (line 25 is the echo $obj->{'S3URL'}; line)

Đây là thân yêu cầu của yêu cầu đến trang:

Request Url: http://localhost:8888/setImage.php
Request Method: POST
Status Code: 200
Params: {
   "S3URL": "http://url.com"
}

Đây là tiêu đề yêu cầu:

Accept: application/json
Content-Type: application/json
Connection: keep-alive
Origin: chrome-extension: //rest-console-id
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML,

Tuy nhiên, nếu tôi thay vào đó lặp lại biến

S3URL=http%3A%2F%2Furl.com
6, tôi sẽ nhận được như sau:

S3URL=http%3A%2F%2Furl.com

Vì vậy, có vẻ như

S3URL=http%3A%2F%2Furl.com
7 đang đọc nó như một chuỗi, và không phải là JSON, điều này sẽ khiến việc phân tích nó trở nên khó khăn hơn.

Bất kỳ ý tưởng tại sao nó không được trả lại dưới dạng JSON, hoặc làm thế nào tôi có thể khiến nó được trả lại dưới dạng JSON?

Hướng dẫn php json = file_get_contents - php json = file_get_contents

S3URL=http%3A%2F%2Furl.com
8
S3URL=http%3A%2F%2Furl.com
6

Request Url: http://localhost:8888/setImage.php
Request Method: POST
Status Code: 200
Params: {
   "S3URL": "http://url.com"
}
7 I am unable to understand this problem ..
Request Url: http://localhost:8888/setImage.php
Request Method: POST
Status Code: 200
Params: {
   "S3URL": "http://url.com"
}
0
Trying to get property of non-object in setImage.php on line 25 (line 25 is the echo $obj->{'S3URL'}; line)
5
S3URL=http%3A%2F%2Furl.com
7
Request Url: http://localhost:8888/setImage.php
Request Method: POST
Status Code: 200
Params: {
   "S3URL": "http://url.com"
}
0
Trying to get property of non-object in setImage.php on line 25 (line 25 is the echo $obj->{'S3URL'}; line)
00
Trying to get property of non-object in setImage.php on line 25 (line 25 is the echo $obj->{'S3URL'}; line)
3 First off, I was able to run this code and it worked fine:
Request Url: http://localhost:8888/setImage.php
Request Method: POST
Status Code: 200
Params: {
   "S3URL": "http://url.com"
}
0
Trying to get property of non-object in setImage.php on line 25 (line 25 is the echo $obj->{'S3URL'}; line)
5
Trying to get property of non-object in setImage.php on line 25 (line 25 is the echo $obj->{'S3URL'}; line)
05 //I ran this curl request against my own php file: curl -i -X PUT -d '{"address":"Sunset Boulevard"}' http://localhost/test.php
I am unable to understand this problem ..
Request Url: http://localhost:8888/setImage.php
Request Method: POST
Status Code: 200
Params: {
   "S3URL": "http://url.com"
}
0
Trying to get property of non-object in setImage.php on line 25 (line 25 is the echo $obj->{'S3URL'}; line)
5
S3URL=http%3A%2F%2Furl.com
7
Request Url: http://localhost:8888/setImage.php
Request Method: POST
Status Code: 200
Params: {
   "S3URL": "http://url.com"
}
0
Trying to get property of non-object in setImage.php on line 25 (line 25 is the echo $obj->{'S3URL'}; line)
00
Trying to get property of non-object in setImage.php on line 25 (line 25 is the echo $obj->{'S3URL'}; line)
3
First off, I was able to run this code and it worked fine:
Request Url: http://localhost:8888/setImage.php
Request Method: POST
Status Code: 200
Params: {
   "S3URL": "http://url.com"
}
0
Trying to get property of non-object in setImage.php on line 25 (line 25 is the echo $obj->{'S3URL'}; line)
5
Trying to get property of non-object in setImage.php on line 25 (line 25 is the echo $obj->{'S3URL'}; line)
05
//I ran this curl request against my own php file:
curl -i -X PUT -d '{"address":"Sunset Boulevard"}' http://localhost/test.php

PHP là ngôn ngữ kịch bản phía máy chủ được thiết kế dành riêng cho phát triển web. Bạn có thể học PHP từ đầu bằng cách làm theo hướng dẫn PHP và các ví dụ PHP này. //get the data $json = file_get_contents("php://input");
//get the data
$json = file_get_contents("php://input");

Vấn đề: Tôi không thể hiểu vấn đề này .. $data = json_decode($json, true);
$data = json_decode($json, true);

Vui lòng gợi ý cho tôi một giải pháp tốt hơn để giải quyết nó ..file_get_contents ('php // input') không hoạt động print_r($data); If that doesn't work, check the console for errors and your php settings:
print_r($data);
If that doesn't work, check the console for errors and your php settings:

  1. Giải pháp: Trước hết, tôi có thể chạy mã này và nó hoạt động tốt:

-terminal ---------- // Tôi đã chạy yêu cầu Curl này đối với tệp PHP của riêng tôi: Curl -i -x put -d '{"địa chỉ": "Đại lộ Sunset" /test.php

  1. --PHP -------------- // Nhận dữ liệu $ json = file_get_contents ("php: // input");

// Chuyển đổi chuỗi dữ liệu thành mảng $ data = json_decode ($ json, true); these php calls.
these php calls.

  1. // xuất mảng trong phản hồi của yêu cầu curl print_r ($ data); Nếu điều đó không hoạt động, hãy kiểm tra bảng điều khiển để biết lỗi và cài đặt PHP của bạn:

URL Curl bạn đã sử dụng, đảm bảo rằng URL thực sự đang hoạt động và indicate either a typo of the "file://input" string or the fact that allow_url_fopen is disabled in php (see #5 if unsure)
indicate either a typo of the "file://input" string or the fact that allow_url_fopen is disabled in php (see #5 if unsure)

  1. không trả về lỗi.

Mở cửa sổ đầu cuối / bảng điều khiển khác và chạy đuôi -f necessarily get underlined in netbeans.
necessarily get underlined in netbeans.

  1. /path/to/the/php/log/file để bạn thực sự có thể thấy đầu ra của các cuộc gọi PHP này.

Để đúng trong cài đặt PHP của bạn. Đó là một cái gì đó được đặt trong php.ini, nhưng bạn cũng có thể thay đổi cài đặt trong thời gian chạy bằng cách viết một cái gì đó dọc theo các dòng của mã sau trước mã khác: ini_set ("allow_url_fopen", true); php.ini, but you can also change settings at run time by writing something along the lines of the following code before the other code: ini_set("allow_url_fopen", true);
php.ini, but you can also change settings at run time by writing
something along the lines of the following code before the other
code:
ini_set("allow_url_fopen", true);

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc

    • Bàn luận: This is a read-only stream that allows us to read raw data from the request body. It returns all the raw data after the HTTP headers of the request, regardless of the content type.
    • Bàn luận: This is a read-only stream that allows us to read raw data from the request body. It returns all the raw data after the HTTP headers of the request, regardless of the content type. function: This function in PHP is used to read a file into a string.
    • Trong bài viết này, chúng ta sẽ xem cách truy xuất bài đăng JSON với PHP, và cũng sẽ thấy việc triển khai của họ thông qua các ví dụ. Đầu tiên, chúng tôi sẽ tìm kiếm các tính năng dưới đây 3: function: This function in PHP is used to read a file into a string. function: This function takes a JSON string and converts it into a PHP variable that may be an array or an object.

    PHP: // Đầu vào: Đây là luồng chỉ đọc cho phép chúng tôi đọc dữ liệu thô từ thân yêu cầu. Nó trả về tất cả các dữ liệu thô sau các tiêu đề HTTP của yêu cầu, bất kể loại nội dung. function: This function takes a JSON string and converts it into a PHP variable that may be an array or an object.$_POST[] global variable. But this fails in the case when we want to receive JSON string as post data. To receive JSON string we can use the “php://input” along with the function file_get_contents() which helps us receive JSON data as a file and read it into a string. Later, we can use the json_decode() function to decode the JSON string.

    file_get_contents () Hàm: Hàm này trong PHP được sử dụng để đọc một tệp vào một chuỗi.$_POST[] global variable. But this fails in the case when we want to receive JSON string as post data. To receive JSON string we can use the “php://input” along with the function file_get_contents() which helps us receive JSON data as a file and read it into a string. Later, we can use the json_decode() function to decode the JSON string.:

    Trying to get property of non-object in setImage.php on line 25 (line 25 is the echo $obj->{'S3URL'}; line)
    
    0

    Chức năng JSON_DECODE (): Hàm này lấy chuỗi JSON và chuyển đổi nó thành biến PHP có thể là một mảng hoặc một đối tượng.: This example uses the json_decode() function that is used to decode a JSON string.

    Xử lý các yêu cầu bài đăng JSON:

    Ví dụ 1: & nbsp; Ví dụ này sử dụng hàm json_decode () được sử dụng để giải mã chuỗi JSON.

    PHP

    Trying to get property of non-object in setImage.php on line 25 (line 25 is the echo $obj->{'S3URL'}; line)
    
    5
    Trying to get property of non-object in setImage.php on line 25 (line 25 is the echo $obj->{'S3URL'}; line)
    
    6
    S3URL=http%3A%2F%2Furl.com
    
    6
    Trying to get property of non-object in setImage.php on line 25 (line 25 is the echo $obj->{'S3URL'}; line)
    
    8

    ________số 8

    Request Url: http://localhost:8888/setImage.php
    Request Method: POST
    Status Code: 200
    Params: {
       "S3URL": "http://url.com"
    }
    
    3

    S3URL=http%3A%2F%2Furl.com
    
    8
    S3URL=http%3A%2F%2Furl.com
    
    6

    Request Url: http://localhost:8888/setImage.php
    Request Method: POST
    Status Code: 200
    Params: {
       "S3URL": "http://url.com"
    }
    
    01
    Trying to get property of non-object in setImage.php on line 25 (line 25 is the echo $obj->{'S3URL'}; line)
    
    2
    Trying to get property of non-object in setImage.php on line 25 (line 25 is the echo $obj->{'S3URL'}; line)
    
    3This example uses the json_decode() function that is used to decode a JSON string.1
    Trying to get property of non-object in setImage.php on line 25 (line 25 is the echo $obj->{'S3URL'}; line)
    
    2
    Trying to get property of non-object in setImage.php on line 25 (line 25 is the echo $obj->{'S3URL'}; line)
    
    3
    This example uses the json_decode() function that is used to decode a JSON string.

    Xử lý các yêu cầu bài đăng JSON:

    Request Url: http://localhost:8888/setImage.php
    Request Method: POST
    Status Code: 200
    Params: {
       "S3URL": "http://url.com"
    }
    
    3

    Ví dụ 1: & nbsp; Ví dụ này sử dụng hàm json_decode () được sử dụng để giải mã chuỗi JSON.

    PHP

    Trying to get property of non-object in setImage.php on line 25 (line 25 is the echo $obj->{'S3URL'}; line)
    
    5
    Trying to get property of non-object in setImage.php on line 25 (line 25 is the echo $obj->{'S3URL'}; line)
    
    6
    S3URL=http%3A%2F%2Furl.com
    
    6
    Trying to get property of non-object in setImage.php on line 25 (line 25 is the echo $obj->{'S3URL'}; line)
    
    8