第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 PHP 上制作如此小的 REACT.JS 代碼?

如何在 PHP 上制作如此小的 REACT.JS 代碼?

慕容3067478 2022-10-08 15:17:54
我在一個基于 php 的 CMS 上創(chuàng)建了一個站點(diǎn)。我自己不了解php,因?yàn)槲沂乔岸碎_發(fā)人員。通常我在 js ( React.js ) 上做網(wǎng)站??紤]到該站點(diǎn)是在 CMS 上編寫的,整個站點(diǎn)只需單擊 CMS 本身管理面板中的按鈕即可完成。但我需要制作一個小的 php 文件,在頁面上顯示特定城市的天氣。最準(zhǔn)確地說,是三個字段和值。我使用API從http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=32ae008b1c7259324aa50450687fabf5&units=metric任務(wù)是在頁面上顯示這個:城市:倫敦溫度:22.05壓力:1012API 本身(除了我不需要的字段)看起來像這樣:{"main":{"temp":22.05,"pressure":1012},"name":"London"}我編寫了顯示天氣的 React.js 代碼:class App extends React.Component {  state = {    city: "",    temp: "",    pressure: ""  }      componentDidMount() {    this.fetchData();  }  fetchData = async () => {    const response = await fetch(`http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=32ae008b1c7259324aa50450687fabf5&units=metric`);    const data = await response.json();    this.setState({      city: data.name,      temp: data.main.temp,      pressure: data.main.pressure    });  };  render() {    return (      <div className="App">        City:{this.state.city}        Temparuture:{this.state.temp}        Pressure:{this.state.pressure}      </div>    );   }  }不幸的是,完成該站點(diǎn)的時間很少,我將無法快速學(xué)習(xí) PHP。也許這里有既了解 PHP 又了解 React.js 的開發(fā)人員,并且能夠幫助我做一些事情。
查看完整描述

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)行示例代碼:

  1. 創(chuàng)建文件所在的index.php文件夾

  2. 將以下代碼復(fù)制到index.php文件中

  3. 在文件夾中運(yùn)行以下命令php -S localhost:8050

  4. http://localhost:8050在您的網(wǎng)絡(luò)瀏覽器中打開


查看完整回答
反對 回復(fù) 2022-10-08
?
寶慕林4294392

TA貢獻(xiàn)2021條經(jīng)驗(yàn) 獲得超8個贊

歸根結(jié)底,您的 React 項(xiàng)目只是一個js文件,因此在您的,在 script 標(biāo)簽中weather_page.php添加該文件。js



查看完整回答
反對 回復(fù) 2022-10-08
  • 2 回答
  • 0 關(guān)注
  • 108 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號