I would like to be able to accomplish the same thing that I do in JS with AJAX all inside PHP. Is this possible?
For example, consider the following code:
$.ajax({
async: false,
url: "/path/to/script/script.php",
type: "post",
data: {
'arg1':'arg_val',
'oper':'get_data',
'arg2':'arg_val_2',
'id_number':'223'
},
dataType: 'json',
success: function(data){
est_data = data[0];
},
error: function(jqXHR, textStatus, errorThrown){
return jqXHR['responseText'];
}
});
Inside PHP I want to do the same thing: pass a few post variables to the script.php and have it return the string response, which I get in the success function in the above code.
I did some research and I thought I should be able to do this using http_post_fields, but I am getting this response:
HTTP/1.1 200 OK Date: Wed, 19 Sep 2012 15:42:01 GMT Server: Apache/2.2.20 (Ubuntu) X-
Powered-By: PHP/5.3.6-13ubuntu3.9 Set-Cookie:
53f143479d91e79747661fcf2777a0fa=5kidtm7rcdn14o33amljgg8922; path=/
Vary: Accept-Encoding Content-Length: 15 Content-Type: text/html not
authorized.
Anyone know how to do this?
Thanks!!
Yes, you need to use file_get_contents() with the help of stream_context_create(). You can also use curl.
Here is an example using file_get_contents: