i am trying to send an array of JSON objects to
PHP server using an hidden field
but all i am getting in the server is just a string
this is my javascript
function CreateArrayOfJSON()
{
var all_childrens = $('#form_div').find('*');//get all root element childrens
var form_elements = {
elements: []
};
for(var i=0;i<all_childrens.length;i++)
{
var id='#'+$(all_childrens[i]).attr('id'); //get id
var style_attr=$(all_childrens[i]).attr('style'); //get style inline
var classes=$(all_childrens[i]).attr("class");
form_elements.elements.push
({
"id" : id,
"style_attr" :style_attr,
"classes" :classes
});
}
document.getElementById('form_elements_array').value=form_elements;//fill hidden field
}
this is my PHP:
this will return Object object (as in the picture above)
$form_elements=$_POST['form_elements_array'];
this will return null
$form_elements=json_decode($_POST['form_elements_array']);
any ideas ?
thank you
document.getElementById('form_elements_array').value=JSON.stringify(form_elements);
can you paste the output string ?
or alternatively
You can use Curl for the same
$url = "URL Where you want to send data";
$ch = curl_init();
$attachment = array(
'name' => 'Your first field',
'link' => 'Second Field',
'description' => 'description here',
);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
$result = curl_exec($ch);
curl_close($ch);
now php code on the receiver page
can you paste the output string ?
or alternatively
You can use Curl for the same
$url = "URL Where you want to send data";
$ch = curl_init();
$attachment = array(
'name' => 'Your first field',
'link' => 'Second Field',
'description' => 'description here',
);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
$result = curl_exec($ch);
curl_close($ch);
now php code on the receiver page
echo $_POST['name']; // get your data with POST method
Related
The below PHP code for Onesignal push notification is working fine,
But how do I add Title to the notification along with a defined custom icon instead of the default bell?
//Push Notifications Starts User --------------------------------------------------------------
notify("Alert","Your Order status has been changed to '$order_status'.");
$content = array("en" => "Your Order status has been changed to '$order_status'.");
$fields = array(
'app_id' => "fa0e5f20-xxxxxxxxxxxxxxxxxxxx",
'included_segments' => array('All'),
'contents' => $content,
'data' => array("en" => "bar"),
'large_icon' =>"ic_launcher_round.png",
);
$fields = json_encode($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8',
'Authorization: Basic NTBjNDExxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$response = curl_exec($ch);
curl_close($ch);
//Push Notifications Ends --------------------------------------------------------------
Please note that "ic_launcher_round.png" is in my studio sdk inside folders "res->mipmap->ic_launcher_round"
The push is working, but I need the Title on top of the content along with 'ic_launcher_round.png' as a custom icon.
Any help is greatly appreciated.
I need to convert this PHP cURL request to JavaScript. I'm not exactly sure how to go about doing it.
I basically need to make this request be client sided instead of server sided.
function createSite($template_id) {
//create array with data
$data = array("template_id"=>$template_id);
//turn data into json to pass via cURL
$data = json_encode($data);
print $data;
//Set cURL parameters
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch,
CURLOPT_URL,'https://api.duda.co/api/sites/multiscreen/create');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, API_USER.':'.API_PASS);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:
application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
//execute cURL call and get template_idte data
$output = curl_exec($ch);
//check for errors in cURL
if(curl_errno($ch)) {
die('Curl error: ' . curl_error($ch));
}
//decode result
$output = json_decode($output);
//return unique site_name
return $output->site_name;
Previously I have posted a similar question on passing JSON data to drop down menu using PHP. How can i arrange data array from API request into HTML SELECT OPTION LIST?
Right now I would like to INSERT the JSON datat in to MySQL database
Below is my api_redcap.php code for calling the data from other web server:
<?php
$data = array(
'token' => '4B0D42AB9D061C0FADD724D2E908349D',
'content' => 'report',
'format' => 'json',
'report_id' => '71',
'rawOrLabel' => 'label',
'rawOrLabelHeaders' => 'label',
'exportCheckboxLabel' => 'false',
'returnFormat' => 'json');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://redcap-virtualbox/redcap/api/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data, '', '&'));
$output = curl_exec($ch);
$json = json_decode($output);
?>
Below is my output from api_redcap.php.:
[{"project_code":"16001","dept":"Orthopaedic Surgery (ORTHO)","group":"","name":"Jameson Lua Yao Chung"},{"project_code":"16002","dept":"Rheumatology Allergy & Immunology (RAI)","group":"","name":"Koh Ee Tzun"},{"project_code":"16003","dept":"Orthopaedic Surgery (ORTHO)","group":"","name":"Ang Wei Luong"},{"project_code":"16004","dept":"Rheumatology Allergy & Immunology (RAI)","group":"","name":"Lim Xin Rong"},{"project_code":"16005","dept":"Physiotherapy (PT)","group":"","name":"Li Kun Man"}]
My table name is called PROJECT_CODE in MySQL database and the attributes are as follow:
Table Name - PROJECT_CODE
Field Name:
PROJECT_code,
DEPARTMENT,
REQUESTOR,
I would like to insert the project_code, dept and name variables from JSON into the MySQL table fields respectively. Please help. Thanks
$json = json_decode($output, true);
foreach($json as $val)
{
$project_code = $val['project_code'];
$dept = $val['dept'];
$name = $val['name'];
/*
* Generate insert query as below
* "INSERT INTO MyGuests (project_code, dept, name) VALUES ($project_code, $dept, $name);";
*/
}
Also, you can insert multiple record in single loop
Please check below reference link
https://www.w3schools.com/php/php_mysql_insert_multiple.asp
In reference link 3 different methods given,so, you can use any one whatever suitable for you
I try to login on axs.com using PHP curl but my script doesn't work.
I want small help to auth this page via PHP.
Is there error in my code?
$username = 'aUsername';
$password = 'aPassword';
$loginUrl = 'https://www.axs.com/login_check';
/* EDIT EMAIL AND PASSWORD */
$EMAIL = $username;
$PASSWORD = $password;
function cURL($url, $header=NULL, $cookie=NULL, $p=NULL)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, $header);
curl_setopt($ch, CURLOPT_NOBODY, $header);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
if ($p) {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $p);
}
$result = curl_exec($ch);
if ($result) {
return $result;
} else {
return curl_error($ch);
}
curl_close($ch);
}
$a = cURL($loginUrl,true,null,"_username=$EMAIL&_password=$PASSWORD");
preg_match('%Set-Cookie: ([^;]+);%',$a,$b);
$c = cURL($loginUrl,true,null,"_username=$EMAIL&_password=$PASSWORD");
preg_match_all('%Set-Cookie: ([^;]+);%',$c,$d);
for($i=0;$i<count($d[0]);$i++)
$cookie.=$d[1][$i].";";
/*
NOW TO JUST OPEN ANOTHER URL EDIT THE FIRST ARGUMENT OF THE FOLLOWING FUNCTION.
TO SEND SOME DATA EDIT THE LAST ARGUMENT.
*/
echo cURL("http://www.axs.com/",null,$cookie,null);
?>
Is this possible on this web site?
I using curl to send request to another page and return value to ajax, i have prevented curl redirect to another and just return value to ajax but final result i receive from ajax have contain html code, my code:
php:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://abc.com');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_MAXREDIRS, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
$data = curl_exec($ch);
curl_close($ch);
echo 'my value';
ajax:
jQuery.ajax({
type: "POST",
url: "http://pagephp.com",
data: data,
success: function(result) {
alert(result);
}
});
alert will show: <html>...</html> my value
help me
You need to enable CURLOPT_RETURNTRANSFER to true/1 then your return value from curl_exec will be the actual result from your successful operation. In other words it will not return TRUE on success. Although it will return FALSE on failure.
http://php.net/manual/en/function.curl-exec.php
You should enable the CURLOPT_FOLLOWLOCATION option for redirects but this would be a problem if your server in in safe_mode and/or open_basedir in effect which can cause issues with curl as well.
header('Content-type: application/json');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://abc.com');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_MAXREDIRS, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
$data = curl_exec($ch);
curl_close($ch);
echo json_encode('my value');
JQUERY
jQuery.ajax({
type: "POST",
url: "http://pagephp.com",
data: data,
success: function(result) {
alert(result);
}
});