如何将 reactJS 前端与 PHP 连接?

How to connect reactJS front-end with PHP?

我正在使用 ReactJS 开发前端。

如何通过 PHP(普通 PHP 或 Yii1)提供此应用程序?

我想您希望通过 HTTP 协议在两者之间创建通信。

在 ReactJSX 中,您想进行一个 fetch/get 调用,该调用由后端的 EndPoint/REST 提供。

// at the beginning of your file
import axios from 'axios';

// in your component constructor
state = {
  yourStateWithTheRecievedData: "",
}

// as a component method
componentDidMount = () => {
  axios
    .get('http://localhost:8080/YourBackendAPI')
    .then(response => {
      this.setState({
        yourStateWithTheRecievedData: response.data.yourStateWithTheRecievedData
      })
      console.log(response.data.yourStateWithTheRecievedData)
    })
}

您现在应该拥有您收到的数据的状态。如果您很难做到这一点,我建议您先将其应用于 public API。 您可以找到 public API 的 here

的列表