Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Ethernet Shield Arduino运行远程PHP脚本_Php_Web_Get_Arduino - Fatal编程技术网

从Ethernet Shield Arduino运行远程PHP脚本

从Ethernet Shield Arduino运行远程PHP脚本,php,web,get,arduino,Php,Web,Get,Arduino,我在外部web服务器上有一个PHP脚本。如果我在浏览器中查看www.example.com/test/myphp.php,服务器运行脚本就很好。现在,我希望服务器在Arduino这样说时运行脚本,而不是在浏览器中查看脚本时。我想让Arduino远程触发脚本运行 我对servers/php/web是一个完全的新手,所以如果有任何提示,尤其是getsynatx(如果这是正确的命令?),我将不胜感激。Arduino有一个以太网屏蔽,可以很好地连接到服务器。下面的代码连接正确,但与我在浏览器中打开www

我在外部web服务器上有一个PHP脚本。如果我在浏览器中查看
www.example.com/test/myphp.php
,服务器运行脚本就很好。现在,我希望服务器在Arduino这样说时运行脚本,而不是在浏览器中查看脚本时。我想让Arduino远程触发脚本运行

我对servers/php/web是一个完全的新手,所以如果有任何提示,尤其是getsynatx(如果这是正确的命令?),我将不胜感激。Arduino有一个以太网屏蔽,可以很好地连接到服务器。下面的代码连接正确,但与我在浏览器中打开
www.example.com/test/myphp.php
时不同,php文件在从Arduino访问时实际上不会运行:(

您对我如何修改它,以便在我的Arduino命令PHP文件运行时让它运行有什么建议吗

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

char server[] = "www.example.com";    

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192,168,0,177);

// Initialize the Ethernet client library
// with the IP address and port of the server 
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
 // Open serial communications and wait for port to open:
  Serial.begin(9600);

  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("SERIAL: Failed to configure Ethernet using DHCP");
  }

  // give the Ethernet shield a second to initialize:
  delay(1000);

  Serial.println("SERIAL: connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {

     client.print("GET /test/myphp.php");
     client.println(" HTTP/1.1");
     client.println("Host: www.example.com");
     client.println("User-Agent: Arduino");
     client.println("Accept: text/html");
     client.println("Connection: close");
     client.println();

  } 
  else {
    // if you didn't get a connection to the server:
    Serial.println("SERIAL: connection failed");
  }
}

void loop()
{
  // if there are incoming bytes available, print them
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    while(true);
  }
}
#包括
#包括
字节mac[]={0xDE,0xAD,0xBE,0xEF,0xFE,0xED};
char服务器[]=“www.example.com”;
//设置DHCP分配失败时要使用的静态IP地址
ip地址ip(192168,0177);
//初始化以太网客户端库
//具有服务器的IP地址和端口
//要连接到的端口(HTTP的默认端口为80):
以太网络客户端;
无效设置(){
//打开串行通信并等待端口打开:
Serial.begin(9600);
//启动以太网连接:
if(以太网开始(mac)==0){
Serial.println(“Serial:无法使用DHCP配置以太网”);
}
//给以太网屏蔽一秒钟进行初始化:
延迟(1000);
Serial.println(“Serial:connecting…”);
//如果您获得连接,请通过串行方式报告:
if(客户端连接(服务器,80)){
print(“GET/test/myphp.php”);
client.println(“HTTP/1.1”);
client.println(“主机:www.example.com”);
client.println(“用户代理:Arduino”);
client.println(“接受:text/html”);
client.println(“连接:关闭”);
client.println();
} 
否则{
//如果您没有连接到服务器:
Serial.println(“Serial:connection failed”);
}
}
void循环()
{
//如果有可用的传入字节,请打印它们
if(client.available()){
char c=client.read();
连续打印(c);
}
//如果服务器已断开连接,请停止客户端:
如果(!client.connected()){
Serial.println();
Serial.println(“断开”);
client.stop();
//永远不做任何事:
虽然(正确);
}
}

对此不确定,但是..我认为您应该在循环中包含这段代码。 不是在外面

 if (client.connect(server, 80)) {

     client.print("GET /test/myphp.php");
     client.println(" HTTP/1.1");
     client.println("Host: www.example.com");
     client.println("User-Agent: Arduino");
     client.println("Accept: text/html");
     client.println("Connection: close");
     client.println();
}

您是否尝试过将用户代理设置为类似Firefox(例如“Mozilla/5.0(Windows NT 6.2;rv:22.0)Gecko/20130405 Firefox/23.0”)?您好,谢谢您的回复!是的,我尝试过。我还尝试过完全省略该字段,这似乎是此人所做的:。您可以通过telnet(在pc上)发送Get,以测试服务器接受的内容。要连接,请使用“telnet www.example.com 80”,绝对最小值为“Get/test/my.php”然后两次返回,现在您的脚本应该运行了。如果不是,服务器的限制性更强,您可能需要使用get发送更多信息。好的,先生,您是我的英雄!这不是错误,而是您所说的(服务器的限制性更强)这让我想到了!!!问题是我忘了用学校的wifi注册我的设备MAC地址,它在我的计算机上工作,但在uC上没有,因为学校阻止了它。它现在对你来说工作完美吗?更新了什么?!我正要建议添加http://