0

How to pass a form variable to another page via JSON? I had create two php file with file naming page_login.php and login.php. However when running login_post.php, the $json value unable pass to login.php

page_login.php:

$email = test@gmail.com;
$password = 123;

$json = array('email' => $email, 'password' => hash("sha256",$password));
$json = json_encode($json);

$ch = curl_init();      
curl_setopt($ch, CURLOPT_URL,"https://localhost/login.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);                                                                                                           
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$server_output = curl_exec($ch);
curl_close ($ch);

login.php:

$json=json_decode($_REQUEST["json"],true);
print $json;
Ichida
  • 319
  • 1
  • 3
  • 12
  • Possible duplicate of [How to POST JSON Data With PHP cURL?](https://stackoverflow.com/questions/11079135/how-to-post-json-data-with-php-curl) – miken32 May 22 '19 at 04:35
  • Need to set Content-Type header and then read `php://input` on the receiving end. – miken32 May 22 '19 at 04:36
  • Or, `curl_setopt($ch, CURLOPT_POSTFIELDS, ['json'=>$json]);` on the sending end. But then you're not passing JSON, just plain old POST value. – miken32 May 22 '19 at 04:38
  • Or, just skip the JSON encoding; on the receiving end look at `$_POST['email']`. Any of these three options will solve your problem. – miken32 May 22 '19 at 04:41
  • May i know why if i send the json data to localhost login.php, its work but if send to another server don't work? – Ichida May 22 '19 at 04:56

1 Answers1

1

Try this

page_login.php

$email = "test@gmail.com";
$password = "123";

$json = array('email' => $email, 'password' => hash("sha256",$password));
$json = json_encode($json);

$ch = curl_init();      
curl_setopt($ch, CURLOPT_URL,"http://localhost/login.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array("json" => $json)));                                                                                                           
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$server_output = curl_exec($ch);
curl_close ($ch);

echo $server_output;

login.php

$json=json_decode($_REQUEST["json"],true);
foreach ($json as $key => $value) {
    echo $key . ": " . $value;
    echo "<BR>";
}
Jayson Garcia
  • 180
  • 1
  • 5
  • 16
  • May i know why if i send the json data to localhost login.php, its work but if send to another server don't work? – Ichida May 22 '19 at 04:48
  • It should work, have you tried getting a response from the server login.php and outputting it in the page_login.php just to verify if the server receives the request? – Jayson Garcia May 22 '19 at 05:05
  • ya, but it just show a blank page, my login php: $json=json_decode($_REQUEST["json"],true); print_r($json); – Ichida May 22 '19 at 05:19
  • @Ichida i did update the page_login.php, can you run that file and replace the `CURLOPT_URL` to the new server – Jayson Garcia May 22 '19 at 05:22
  • On your login.php, can you do this`echo "
    "; var_dump($_REQUEST); echo "
    "; $json=json_decode($_REQUEST["json"],true); print_r($json);`
    – Jayson Garcia May 22 '19 at 05:25
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/193741/discussion-between-jayson-garcia-and-ichida). – Jayson Garcia May 22 '19 at 05:26