The secret to wholesale...

In the article about buying Thai goods, I introduced wholesale markets and cheap...

Restaurant business and the...

Customers are a special figure in restaurants. This may seem obvious, but...

Guide the clues to...

Japanese goods Known for putting quality first. However, in the past, due...

Online cosmetics business and...

Starting a business, especially online cosmetics business is the silver trend and...
HomeLập trìnhPhpSự cố khi...

Sự cố khi sử dụng JSON qua PHP để truy cập openweather-API


Xin chào ,

Tôi đang cố truy cập openweather-API. Hầu hết dữ liệu tôi có thể truy cập, nhưng chỉ một số dữ liệu tôi không thể truy cập – ví dụ: “mô tả”:

$currDataPolled          = file_get_contents("http://api.openweathermap.org/data/2.5/weather?id=2939623&appid=xxxxxxxxxxxxxxx&units=metric&lang=de");
$currUvIndexPolled       = file_get_contents("http://api.openweathermap.org/data/2.5/uvi?appid=xxxxxxxxxx&lat=48.26&lon=11.43");
//parsing
$parsedCurrData          = json_decode($currDataPolled);
$parsedcurrUvIndex       = json_decode($currUvIndexPolled);
//getting values to vars
$location                = $parsedCurrData->{'name'};
$currTemp                = $parsedCurrData->{'main'}->{'temp'};
$currHumid               = $parsedCurrData->{'main'}->{'humidity'};
//$currPressure          = $parsedCurrData->{'main'}->{'pressure'};
$currDescription         = $parsedCurrData->{'weather'}->{'0'}->{'description'};//->{'id'};
$currUv                  = $parsedcurrUvIndex->{'value'};
//outputting
echo $currDescription."<br />\n";

Trong trình duyệt web, cuộc gọi API cho kết quả như sau:

{
  "coord": {
    "lon": 11.43,
    "lat": 48.26
  },
  "weather": [
    {
      "id": 500,
      "main": "Rain",
      "description": "Leichter Regen",
      "icon": "10n"
    }
  ],
  "base": "stations",
  "main": {
    "temp": 16.66,
    "pressure": 1014,
    "humidity": 93,
    "temp_min": 15,
    "temp_max": 18
  },

Có phải vì “[” and “]” ?

Cảm ơn bạn rất nhiều!


Tôi đã chỉnh sửa bài đăng của bạn để dễ đọc. Khi bạn nhập một khối mã vào diễn đàn, hãy đặt trước nó một dòng có ba dấu gạch ngược và theo sau nó là một dòng có ba dấu gạch ngược để dễ đọc hơn. Xem bài đăng này để tìm backtick trên bàn phím của bạn. Công cụ “văn bản được định dạng sẵn” trong trình chỉnh sửa (</>) cũng sẽ thêm dấu ngược xung quanh văn bản.

markdown_Forums

Cảm ơn bạn đã chỉnh sửa và giải thích.

Trong khi chờ đợi, tôi đã tìm ra giải pháp: Tôi không nhận ra “thời tiết” là một mảng. Điều này đã làm việc:

$currDescription         = $parsedCurrData->weather[0]->description;

Đọc thêm  PHP - Không tìm thấy Dotenv?



Zik.vn – Biên dịch & Biên soạn Lại

spot_img

Create a website from scratch

Just drag and drop elements in a page to get started with Newspaper Theme.

Buy Now ⟶

Bài viết liên quang

DMCA.com Protection Status