2 回答

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超3個贊
您可以輕松地使用file_get_contents來獲取響應(yīng)并將json_decode其轉(zhuǎn)換為對象。
請以后自己嘗試一下,因?yàn)檫@不是一個總是滴灌整個答案的社區(qū)。我個人也不建議在生產(chǎn)中使用此代碼。
<?php
$response = file_get_contents("http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=32ae008b1c7259324aa50450687fabf5&units=metric");
$weather = json_decode($response);
?>
<div>City: <?php echo $weather->name ?></div>
<div>Temparuture: <?php echo $weather->main->temp ?></div>
<div>Pressure: <?php echo $weather->main->pressure ?></div>
相關(guān)文章:
https://www.php.net/manual/en/function.file-get-contents.php https://www.php.net/manual/en/function.json-decode.php
如果您在本地安裝了 php,則可以按照以下方式運(yùn)行示例代碼:
創(chuàng)建文件所在的
index.php
文件夾將以下代碼復(fù)制到
index.php
文件中在文件夾中運(yùn)行以下命令
php -S localhost:8050
http://localhost:8050
在您的網(wǎng)絡(luò)瀏覽器中打開

TA貢獻(xiàn)2021條經(jīng)驗(yàn) 獲得超8個贊
歸根結(jié)底,您的 React 項(xiàng)目只是一個js
文件,因此在您的,在 script 標(biāo)簽中weather_page.php
添加該文件。js
添加回答
舉報