BR
asked on
Sending JSON Via POST In PHP
Dear Experts,
I'm new with sending data in Json format,
Reading posted data in Json format is ok for me. ( Thanks to Ray Paseur )
I use below code on my json.php page. When I visit json.php page, I'm expecting the code runs and the page posts $jsonData to the myurl.php . Normally when I post a data with html form, my page is going to action page, and I'm expecting here the same, I'm waiting to go myurl.php but json.php goes nowhere and it gives no error.
PHP works when a page is called, right? So that I guess, it doesn't post anything, right?
I'm using shared hosting with PHP 5.6
//the code is here: http://thisinterestsme.com/sending-json-via-post-php/
I'm new with sending data in Json format,
Reading posted data in Json format is ok for me. ( Thanks to Ray Paseur )
I use below code on my json.php page. When I visit json.php page, I'm expecting the code runs and the page posts $jsonData to the myurl.php . Normally when I post a data with html form, my page is going to action page, and I'm expecting here the same, I'm waiting to go myurl.php but json.php goes nowhere and it gives no error.
PHP works when a page is called, right? So that I guess, it doesn't post anything, right?
I'm using shared hosting with PHP 5.6
<?php
//API Url
$url = 'myurl.php;
//Initiate cURL.
$ch = curl_init($url);
//The JSON data.
$jsonData = array(
'username' => 'MyUsername',
'password' => 'MyPassword'
);
//Encode the array into JSON.
$jsonDataEncoded = json_encode($jsonData);
//Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_POST, 1);
//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
//Execute the request
$result = curl_exec($ch);
//the code is here: http://thisinterestsme.com/sending-json-via-post-php/
ASKER
Dear Ray Paseur,
thank you very much. I will read the article again.
I need to ask this, it's like a splinter in my mind..
when I post the data, the data should be posted to this url, right?
$url = "https://Iconoun.com/demo/request_reflector.php";
so when I run your code, the page should post the name & mail to https://Iconoun.com/demo/request_reflector.php , right?
but the page brings lots of information and stays on the same page?
Let me read the article first, then write you again. Thank you
thank you very much. I will read the article again.
I need to ask this, it's like a splinter in my mind..
when I post the data, the data should be posted to this url, right?
$url = "https://Iconoun.com/demo/request_reflector.php";
so when I run your code, the page should post the name & mail to https://Iconoun.com/demo/request_reflector.php , right?
but the page brings lots of information and stays on the same page?
Let me read the article first, then write you again. Thank you
Yes, you should be able to make the POST request to https://iconoun.com/demo/request_reflector.php
Here is the request reflector script.
Here is the request reflector script.
<?php // demo/request_reflector.php
/**
* Bounce the request vars back
*/
error_reporting(E_ALL);
date_default_timezone_set('America/Chicago');
// START A BUFFER TO CAPTURE THE BROWSER OUTPUT
ob_start();
// USE PREFORMATTING TO MAKE THE OUTPUT EASY TO READ
echo '<pre>';
// SHOW THE COOKIE(S)
echo '$_COOKIE: ';
var_dump($_COOKIE);
echo PHP_EOL;
// SHOW THE GET REQUEST
echo '$_GET: ';
var_dump($_GET);
echo PHP_EOL;
// SHOW WHAT WAS RECEIVED IN POST
echo '$_POST: ';
var_dump($_POST);
echo PHP_EOL;
// SHOW WHAT WAS RECEIVED IN FILES
echo '$_FILES: ';
var_dump($_FILES);
echo PHP_EOL;
// CAPTURE THE BUFFER
$request_data = ob_get_clean();
// SAY THANK YOU
$refer = !empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '(empty referrer)';
echo 'REQUEST REFLECTED FROM ' . $refer . ' AT ' . date('c');
echo PHP_EOL;
echo $request_data;
The additional information you are seeing is just stuff from cURL. If you don't want it, you can remove the instructions that print this information (lines 29-36). I used it mostly for debugging, and for teaching how to use cURL.
ASKER
Dear Ray Paseur, thank you again.
I saved the first code as json.php and run the page, but it didn't go to request_reflector.php page.
I just copy and past the code to be sure that I didn't break anything, but the page doesn't go anywhere..
the page stays on the same url as json.php .
when I run the page, it is not going to request_reflector.php page.
Thank you for your patience...
I saved the first code as json.php and run the page, but it didn't go to request_reflector.php page.
I just copy and past the code to be sure that I didn't break anything, but the page doesn't go anywhere..
the page stays on the same url as json.php .
when I run the page, it is not going to request_reflector.php page.
Thank you for your patience...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Now I get it :)
Thank you so much
I think i can use it also for Hand Shake if I need it
Thank you so much
Thank you so much
I think i can use it also for Hand Shake if I need it
Thank you so much
ASKER
thank you so much
Here's the discussion of JSON in PHP
https://www.experts-exchange.com/articles/22519/Understanding-JSON-in-PHP-and-JavaScript-Applications.html
Here's a script that makes a POST-method request. I use this as my template when talking to APIs that expect POST. You can install it and use it to post information to my request reflector script.
Open in new window