1. JS-->PHP
a). JS create Json
<script>
$(document).ready(function(){
/*--JS create Json--*/
var jsonObject={}; // In another way: jsonObject={'name':"Bruce",'age':25};
jsonObject['name'] = "Bruce";
jsonObject['age'] = 25;
console.log(jsonObject);
console.log('This is stringfied json object: ' + JSON.stringify(jsonObject));
console.log(JSON.parse(JSON.stringify(jsonObject)));
$("#demo").html(jsonObject.name + ", " +jsonObject.age);
/*--JS create Json--*/ });
</script>
Js code create json array object
b). Pass Json from JS to PHP by using Ajax
<script>
$(document).ready(function(){
/*--JS create Json--*/
var jsonObject={}; // In another way: jsonObject={'name':"Bruce",'age':25};
jsonObject['name'] = "Bruce";
jsonObject['age'] = 25;
console.log(jsonObject);
console.log('This is stringfied json object: ' + JSON.stringify(jsonObject));
console.log(JSON.parse(JSON.stringify(jsonObject)));
$("#demo").html(jsonObject.name + ", " +jsonObject.age);
/*--JS create Json--*/ /*--Ajax pass data to php--*/
$.ajax({
url: 'php/test.php',
type: 'POST', //or use type: 'GET', then use $_GET['json'] or $_POST['json'] to in PHP script
data: { json: JSON.stringify(jsonObject)},
success: function(response) {
console.log(response);
var jsonObj = JSON.parse(response);
$("#demo").html("From PHP's echo: " + jsonObj.name + ", " + jsonObj.age);
}
});
/*--Ajax pass data to php--*/ });
</script>
JS side
<script>
$(document).ready(function(){
/*--JS create Json--*/
var jsonObject={}; // In another way: jsonObject={'name':"Bruce",'age':25};
jsonObject['name'] = "Bruce";
jsonObject['age'] = 25;
console.log(jsonObject);
console.log('This is stringfied json object: ' + JSON.stringify(jsonObject));
console.log(JSON.parse(JSON.stringify(jsonObject)));
$("#demo").html(jsonObject.name + ", " +jsonObject.age);
/*--JS create Json--*/ /*--Ajax pass data to php--*/
$.ajax({
url: 'php/test.php',
type: 'POST', //or use type: 'GET', then use $_GET['json'] or $_POST['json'] to in PHP script
data: { json: JSON.stringify(jsonObject)},
success: function(response) {
console.log(response);
var jsonObj = JSON.parse(response);
$("#demo").html("From PHP's echo: " + jsonObj.name + ", " + jsonObj.age);
}
});
/*--Ajax pass data to php--*/ });
</script>
PHP side
2. PHP-->JS
a). PHP create Json
<?php $arr = array(
'name' => "Bruce",
'age' => 25,
);
echo json_encode($arr); // {"name":"Bruce","age":25}
echo $arr['name']; // Bruce
echo JSON_decode(json_encode($arr))->{'name'};// Bruce
echo implode((array)json_encode($arr)); // {"name":"Bruce","age":25} ?>
PHP code
b). PHP cURL Call RESTful web service
<?php try {
$data = $_POST['json'];
//echo $data; try {
$rest_url = "";
//echo $rest_url;
//$host = array("Content-Type: application/json; charset=utf-8");
$header = array(
'Content-Type: application/json',
'Centent-Length:'.strlen($data)
//'Content-Disposition: attachment; filename=template1.xlsx'
); $ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_URL, $rest_url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch); echo $output;
} catch (Exception $e) {
echo $e -> getMessage();
} }catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
} ?>
PHP cURL code
3. Pass Json from PHP to PHP (must be array then json_encode('json string')?)
http://*.com/questions/871858/php-pass-variable-to-next-page
4. Submit parameters to PHP through HTML form POST/GET to download a file (e.g. Excel...)
I figure out a way around this. Instead of making a POST call to force the browser to open the save dialog, I will make a POST call to generate the file, then temporary store the file on the server, return the filename . Then use a GET call for this file with "Content-Disposition: attachment; filename=filename1". The GET call with that header will force the browser to open the "Save this file" dialog, always.
<?php
require_once 'RESTClient.php';
$url = 'http://158.132.51.202/SWR-SHRS/API/V1/';
//echo $url; $type = 1;
if(!empty($_GET['type'])){
$type = trim($_GET['type']);
}
$data = $_GET['filter']; $client = new SHRRESTClient($url);
$path = $client->downloadExcel($dataId['studid'], (array)json_decode($data));
if($type == 0){
echo "http://localhost/php/".$path;
}else{
// send header information to browser
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;');
header('Content-Disposition: attachment; filename="helpers_list.xlsx"');
header('Content-Length: ' . filesize($path));
header('Expires: 0');
header('Cache-Control: max-age=0');
//stream file
flush();
print file_get_contents($path);
unlink($path); //delete the php server side excel data
} ?>
exportFile.php
<?php class SHRRESTClient{ public $base_url = null;
public function __construct($base_url = null)
{
if (!extension_loaded('curl')) {
throw new \ErrorException('cURL library is not loaded');
}
$this->base_url = $base_url;
} public function downloadExcel($sid, $data){
$url = $this->base_url.$sid.'/...url...';
$data_string = json_encode($data);
# open file to write
$path = 'tmp/'.$sid.'.xlsx';
$fp = fopen ($path, 'w+');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
# write data to local file
curl_setopt($ch, CURLOPT_FILE, $fp );
$result = curl_exec($ch);
# close local file
fclose( $fp );
curl_close($ch);
return $path;
}
}
?>
RESTClient.php