Jump to content

Proper HTTP request


Nicknasty
 Share

Recommended Posts

Hi Guys, 

Total PHP Noob here. I’ve run into a brick-wall and would appreciate some words of wisdom. 
I am working on a fairly complicated (at least to my skill level..)  home automation/IOT -project and unexpectedly have a big problem establishing a connection between the Internet and my home network. I’ve spent too many hours trying to solve this and eventually have reverted to a super basic Hello World -example to highlight the issue.

I can do this with a browser:

http://86.60.161.24  // will echo hello World

I can’t do this with my server:

<?php
  $url = 'http://86.60.161.24';
  $contents = file_get_contents($url);
  echo ($contents);
?>

It looks like the server-side HTTP -request never “touches” my Arduino-chip. No network activity detected. Obviously there is going to be a lot more data transfer happening but I need to solve this issue first. 

Is there anything I can do to make this work? Any and all input greatly appreciated. Cheers!!

 

Link to comment
Share on other sites

Be more specific. This doesn't work does not help us. What error message are you getting? What is getting echo'ed? Do you have error reporting turned on?

error_reporting(E_ALL);

P.S. Remove the () in the echo.

Link to comment
Share on other sites

Thanks for helping me out gw1500se,

Rather unsurprisingly, my php server fails to open the stream. 

WARNING:file_get_contents(http://86.60.161.24): failed to open stream: Connection timed out 

I can access this ip with a browser. Some mysterious force is keeping the pipes shut. Nothing get's echoed and it takes a long time for the script to finish. My php server can access everything else, like cnn.com etc. with the same script. 

Link to comment
Share on other sites

Is your server that is running the code being hosted somewhere outside your network?  Maybe you need to setup some port forwarding rules on your router or enable the port in a firewall somewhere.

 

Link to comment
Share on other sites

2 hours ago, kicken said:

Is your server that is running the code being hosted somewhere outside your network?  Maybe you need to setup some port forwarding rules on your router or enable the port in a firewall somewhere.

 

I have port forwarding in place and it is working fine. You should get Hello World by accessing the url 86.60.161.24. No such luck with php retrieval

Link to comment
Share on other sites

3 hours ago, Nicknasty said:

You should get Hello World by accessing the url

Like Barand, it times out for me also.  The remote host does not respond to the the connection attempt.  Wireshark shows all the outgoing SYN packets but no replies.

Sounds like you have a networking issue.  Either your port forwarding/firewall configuration is not setup properly or your ISP may be blocking the port.   If you're fairly sure your forward/firewall is correct try different port numbers.

Link to comment
Share on other sites

5 hours ago, kicken said:

Like Barand, it times out for me also.  The remote host does not respond to the the connection attempt.  Wireshark shows all the outgoing SYN packets but no replies.

Sounds like you have a networking issue.  Either your port forwarding/firewall configuration is not setup properly or your ISP may be blocking the port.   If you're fairly sure your forward/firewall is correct try different port numbers.

I am seeing a fair amount of traffic so someone from the forum is getting through. I can access this url with my mobile (wifi turned off) but not with my ipad. 

I am investigating three different problems:

- Some clients can reach URL 86.60.161.24, some can't. Why is that?

- Is my Arduino board replying to http requests properly or is this the part that throws things off for some clients and my php server

- Is there some restriction / do I have correct fopen wrappers enabled at my PHP server as Barand suggested.

 

will continue solving the problem

 

btw, my PHP server is hosted by Bluehost and I am operating it remotely.

Screenshot 2021-03-27 at 8.42.16.png

Link to comment
Share on other sites

1 hour ago, Nicknasty said:

Some clients can reach URL 86.60.161.24, some can't. Why is that?

Unknown. 

I just tried again and this time it connected successfully.  So either your Arduino was simply off when I tried before, or the request is only sometimes blocked.

1 hour ago, Nicknasty said:

Is my Arduino board replying to http requests properly or is this the part that throws things off for some clients and my php server

The fact that it is working now but wasn't before means it's possible you have a problem in your Arduino code.  Are you using a library to handle the TCP/IP stuff and HTTP stuff or is it custom? 

You'll need to spend some time analyzing your setup trying to determine why there are connection problems.  If it's possible to SSH into your external bluehost server that would be helpful as you could SSH in and try connecting with curl and get better feedback.

1 hour ago, Nicknasty said:

Is there some restriction / do I have correct fopen wrappers enabled at my PHP server as Barand suggested.

I would guess your PHP configuration is fine, but you can use phpinfo() to check the settings. 

Link to comment
Share on other sites

Have to say I am fairly disappointed with the Phpfreaks forum. I am getting very little advise (thanks anyway for those who participated) but instead seeing 20-30 very childish hacking attempts trying to breach my Arduino board. Shame on you. I'll be closing down this thread and switch my IP so it is sayonara MF's!! 

If anyone is still interested of helping me instead of just trying to hack me I have one question: Are you able to machine read the current output of http://86.60.161.24 with PHP? If yes, what methods do you use?

 

 

Link to comment
Share on other sites

You're in luck - it didn't time out on me this time

<?php
$text = file_get_contents('http://86.60.161.24');
echo "<pre>$text</pre>"; 
?>

Outout

analog input 0 is 556

analog input 1 is 465

analog input 2 is 424

analog input 3 is 228

analog input 4 is 364

analog input 5 is 310

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.