I have done some research regarding on how to use file_get_content
with post. And I have also read this one which is I honestly don't understand since I am not that familiar with PHP. Below is my php code in getting my json and used it for my ajax request, using methog GET
.:
<?php
echo(file_get_contents("http://localhost:8001/" . $_GET["path"] . "?json=" . urlencode($_GET["json"])));
?>
Now, I am using method POST
and I dont know how to modify my php code to post my data from my javascript. Below is my data
which I wanted to post in my url request (that is also what I used as json
in method GET
):
{"SessionID":"9SQLF17XcFu0MTdj5n",
"operation":"add",
"transaction_date":"2011-7-28T00:00:00",
"supplier_id":"10000000108",
"wood_specie_id":"1",
"lines": [{"...":"...","..":"..."},{"...":"...","..":"..."}],
"scaled_by":"SCALED BY",
"tallied_by":"TALLIED BY",
"checked_by":"CHECKED BY",
"total_bdft":"23.33",
"final":"N"}
I just need to change this code
echo(file_get_contents("http://localhost:8001/" . $_GET["path"] . "?json=" . urlencode($_GET["json"])));
with POST
to send my post my data.
EDIT:
I need to produce a request like this:
http://localhost/jQueryStudy/RamagalHTML/processjson.php?path=getData/supplier?json={"SessionID":"KozebJ4SFqdqsJtRpG6t1o3uQxgoeLjT"%2C"dataType":"data"}
You can pass a Stream Context as the third argument to file_get_contents
. With the Stream Context, you can influence how the HTTP request will be made, e.g. you can change the Method, add Content or arbirtrary headers.
file_get_contents($url, false, stream_context_create(
array (
'http' => array(
'method'=>'POST',
'header' => "Connection: close\r\nContent-Length: $data_len\r\n",
'content'=>$data_url
)
)
));
After each request, PHP will automatically populate the $http_response_header
which will contain all the information about the request, e.g. Status Code and stuff.
$data_url = http_build_query (array('json' => $_GET["json"]));
$data_len = strlen ($data_url);
echo file_get_contents("http://localhost:8001/" . $_GET["path"], false, stream_context_create(
array (
'http' => array(
'method'=>'POST',
'header' => "Connection: close\r\nContent-Length: $data_len\r\n",
'content'=>$data_url
)
)
));
What you need is cURL.
Example:
$dataString = "firstName=John&lastname=Smith";
$ch = curl_init();
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,2); // number of variables
curl_setopt($ch,CURLOPT_POSTFIELDS,$dataString);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
If i understand you correctly(I might not) you should use CURL.
CURL is the way to submit POST requests within PHP. (but it is not the only way)
What you are doing is sending the data by the GET method
some think like this, please read about it, this one will not work out of the box
<?php
$ch = curl_init("http://localhost:8001/" . $_GET["path"] );
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, "json=".urlencode($_GET["json"]));
curl_exec ($ch);
curl_close ($ch);
?>