send command via php
I have a webpage index.php from where I would like to send a command through another url but without going to this page. This is my code:
<?php
$nb = $_GET[‘value’];
if ($nb >= 1000)
header('Location: http://ipaddress/.../?command=on');
else if $nb >= 500 && $nb < 1000)
header('Location: http://ipaddress/.../?command=middle');
else
header('Location: http://ipaddress/.../?command=off');
?>
The problem is that the p开发者_JAVA百科age tries to change due to the “header”, I just would like to send the command but to stay on my page index.php Is there any way to do that?
Thanks a lot!
Use curl
You need to use curl
(man page) for this.
POST Example
For example to post a command on to the page you would use:
<?php
$ch = curl_init('http://ipaddress');
$data = array('command' => 'middle');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);
?>
GET example
<?php
$ch = curl_init('http://ipaddress?command=middle');
curl_exec($ch);
?>
Preventing the requested URL's content from being output to screen
You need to look at the CURLOPT_RETURNTRANSFER
option of curl_setopt()
. For example:
<?php
$ch = curl_init('http://ipaddress?command=middle');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$return = curl_exec($ch);
?>
Now $return
contains the content fromthe requested URL
Documentation
The options used in curl_setopt()
are documented here.
If it's fine that it's the server doing the call, use e.g. file_get_contents()
to send the command.
If you need the client to make the request, you could e.g. serve an img
or iframe
element and load the target URL into that.
If I understand correctly, use file_get_contents instead:
<?php
$nb = $_GET[‘value’];
if ($nb >= 1000)
file_get_contents('http://ipaddress/.../?command=on');
else if $nb >= 500 && $nb < 1000)
file_get_contents('http://ipaddress/.../?command=middle');
else
file_get_contents('http://ipaddress/.../?command=off');
?>
Yes, you could use Ajax for that. Ajax is better than cURL or file_get_contents since you will stay on your page, exactly where you are.
function SendData(strData)
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else {// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
// The page has been requested and your code has been run
}
xmlhttp.open("GET","http://ipaddress/.../?command=" + strData, true);
xmlhttp.send();
}
}
Call it with HTML:
<a href='#' onClick='SendData("on");'>Send ON</a>
It's possible, by setting up a cURL session from your script - but don't you rather want to include the file and call a function or something?
Yes, you should look at using cURL.
file_get_contents() for GET request or cURL for GET and POST requests
Yup.
$resp = file_get_contents('http://ggg.com?foo=bar');
Check out the PHP manual for file_get_contents and fopen which have wrappers for fetching http content, enabled by default in most builds.
You can even POST using this method without having to use additional libraries like cURL. If you think this functionality might be useful to you, have a look at stream_get_contents in the PHP manual.
精彩评论