Suppose I have test.php file which echo the value of user and data .Now I can easily check my test.php whether it is working fine or not from a HTML form with user and data
input. But I want to check my test.php file without html form.
Is there any tool or browser plugin to POST or to GET data from test.php.
//test.php.
<?php
echo $_POST['user'];
echo $_POST['data'];
?>
Thanks in advance
Update I have found RESTclient and its really fine for me.
Your best bet is to simply watch webpages interact with your service using Fiddler; you'll then understand what format the service expects.
To send requests in Fiddler, click the Composer tab and fill in the headers and body.
Its fairly straightforward to execute a POST request from the CLI using cURL
curl --data "user=sharif&data=HelloWorld" http://site.com
See also curl usage explained
Related
I create to file ajax.php and jajaxget.php.
script in ajax.php is-
<script>
var data={name:"nikhil"};
$.ajax({type:"post",url:"jajaxget.php",data: {name:"nikhil"},success:function(r,s){alert(s);alert(r);}});
</script>
and jajaxget.php is
<!doctype html>
<html>
<body>
<?php
echo $_POST["name"];
?>
ajax.php works fine and in alert i can see the data which i send. I get a success alert and this ALERT
But when i open the get.php file in browser there is nothing . "nikhil" should show on the page. but the page is blank . why data is not recived by jajaxget.php file. It may be very foolish question .but please help
That should not show on the page when you open it on the browser because you are not passing any POST data when you do so. Your code is working as expected. I think your problem is you do not really get to understand how GET/POST works.
In the post environment variable the post data sent on request is stored. It is not stored forever nor as kind of a cookie. When you make another request, the variable will have different data according with the headers of the request.
So when you are using AJAX and send post data to your file, the return shows the "name" value you've passed because you've sent it on the request headers. However, when you request the page on a browser and you are not passing any POST data, that won't be shown, because nothing you've sent.
If you want your server to store that data, you may use databases or cookies for that.
For more information read some webpages online like http://www.w3schools.com/tags/ref_httpmethods.asp. And please try to understand a bit how the web works before filing it of low quality questions.
If you still have doubts, please ask me.
when use GET Method for receive JSON data , we can acsses the result directly from web browser , for example i send a mydata value from ajax to a main.php file and it process and get answer show a result some thing like below :
<?php
if (isset($_GET["mydata"])) {
if ($_GET["mydata"]=="hello"){
echo "hello world";
}
}
?>
but when a user call it in browser directly like http:mysite.com/mydata.php?mydata=hello recive answer . i want dont allow users to get answer of http request directly , and just can show it from ajax result of main page is it possible ?
You're asking how to prevent an ajax-only request from being accessed directly by copy-pasting the URL into the web browser; that is, only allowing the URL to be accessible via ajax on the main web page.
Well, there are a few things you can try:
Check the Referrer for the URL of the main page with $_SERVER['HTTP_REFERER']
Set a header in Javascript using xhr.setRequestHeader() and then ensure it's value by checking for $_SERVER['HTTP_X_....'] in PHP
Like Jay Bhatt recommended, check for the X_REQUESTED_WITH header, but be aware this might not always be set (see: X-Requested-With header not set in jquery ajaxForm plugin)
However, in any of these situations you should be aware that anyone who knows what they are doing can easily set any HTTP header, variable, or even modify the referrer which is sent to the server. As such, there is no 100% guarantee that your resouce can be accessed only via AJAX on the main web page. There is no control built in the internet to verify where a request is coming from, so anyone can easily spoof or fake it.
I am a php newbie and I came across some problems when use php with apache.
I don't want to use browser to send or receive http request so that I have to manually deal with this problem. On server side, I can use file_get_content("php://input") to extract body from the http request, but how can I build the http response? The method is "post" and I want to insert a xml string to the response body. Thank you for help!
Example:
See: http://php.net/manual/en/function.header.php
For HTTP codes, Google them up.
Simply echo "whatever" will return that back to the users browser. Remember that PHP is an interpreted language originally for inline scripting (i.e. mixing with HTML) prior to sending to the browser. So a file:
<?php echo "here"; ?>
will just return that reply to the users browser when they go to the appropriate URL. Adding any parsing etc. can be done in addition to this basic application logic.
I want to post to an external php file and get the result. It a php that i have hosted in my server online. I want the static page in my localhost post by ajax and load the html in a div. But I'm not able to do this.
$.post("http://www.site.com/index.php", { font: "panchami", input: "hi" } );
Is there anything wrong in this?
The Same Origin Policy prevents Ajax calls to external domains.
Popular workarounds include
JSONP
Embedding the data in an iframe instead
Using a server-side proxy the does the fetching (see #BrunoLM's answer for a PHP example; it is possible in any server-side language)
YUI's Get as shown in #Alex's answer
depending on what your use case is.
Javascript doesn't allow cross domain requests.
What you can do is a PHP file on your server that reads the contents of the other site:
<?php echo file_get_contents($_REQUEST['url']); ?>
Then make requests to your file, like so:
$.post("proxy.php?url=external_url", ...);
Or using GET, for example:
http://developer.yahoo.com/yui/get/
This kind of request is dangerous, it is called a Cross-Site request and is forbidden by most browsers. If you look in your error console you should see a message to that effect.
If you really have no alternative then you can consider using iframes, the src attribute can be outside the current domain and you can parse the information using javascript.
Hope that helps :)
I am using the script found on this page.
I downloaded the whole package found on the page and modified the url to the php file like this:
$('#rate').rating('output.php', {maxvalue:5});
And in the output.php I have this. The author of the page says that the post will be calling rating. So that is why I'm using post rating.
<?php
if(isset($_POST['rating'])) {
header("Location: http://www.google.com");
}
?>
But when I run the page and select a few stars nothing happens. The reason why I did header location because I wanted to see if the thing works. But it's not working for me. Do you guys know what I could be doing wrong?
What do you expect it to do? By doing a header Location of google.com, you are essentially redirecting the AJAX request, not the browser of the user that is making the call. You could just print "test" and check with Firebug that the request was made and the text was returned.
Your're sending an ajax request. So you're browser won't redirect.
You could use firebug a firefox plugin. You can check you're ajax requests there, and what they return on the console tab.
Plus you're using jquery make sure you're javascript is correct!
As mentioned above, use Firebug's excellent debugging console to check what response you get. The proper way to validate an ajax request is to check if the HTTP_X_REQUESTED_WITH key in the superglobal server array contains XMLHttpRequest. I. e, you could do this:
<?php
function isAjaxRequest(){
return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest';
}
if(isAjaxRequest(){
echo 'Hello, Ajax!';
}
else exit;
?>
Best,
Christian
How do you know nothing is working? Download Firebug, http://getfirebug.net, go to the "NET" tab and do your ajax request. Then check what kind of response you get. You also have to process the data you get from the rating script! You only get the javascript bit, the server side coding is up to you! You could i.e. do a var_dump of the 'RATING' post variable, and debug it with Firebug. Best of luck!