简体   繁体   English

如何在 React Native 中连接 PHP 文件?

[英]How to connect with PHP file in react native?

I am connecting in react native with php files (Back end) like this:我正在与 php 文件(后端)进行本机反应,如下所示:

    componentDidMount(){
    return fetch('http://192.168.1.7:80/Encaissement.php',
    .....}

but the adresse ip is changing every time, so how to get directley current ip adress?(PS: I'm not using localhost because it gets me error with network)但是地址 ip 每次都在变化,那么如何直接获取当前 ip 地址?(PS:我没有使用本地主机,因为它让我的网络出错)

OR how to replace 192.168.1.7 as variable and give it a valeur just in one page (because I have lot of pages and when I need to change ip adress I had to change it in like 15 pages)或者如何将 192.168.1.7 替换为变量并在一页中赋予它一个价值(因为我有很多页面,当我需要更改 ip 地址时,我不得不将其更改为 15 页)

To configure just in one site you can create a component and include it in your code when you need to fetch the data.要仅在一个站点中进行配置,您可以创建一个组件并在需要获取数据时将其包含在代码中。

File GetIp.js文件 GetIp.js

const getIp = () => { return 'http://192.168.1.7:80'; }

export default getIp;

Then when you need it yous just need to import it and use it然后当你需要它时,你只需要导入它并使用它

import getIp from './GetIp';

...
componentDidMount(){
  fetch(getIp() + '/Encaissement.php');   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM