简体   繁体   English

通过带有CURL的JSON传递变量

[英]Pass Variable Via JSON with CURL

How to pass a form variable to another page via JSON? 如何通过JSON将表单变量传递给另一个页面? I had create two php file with file naming page_login.php and login.php. 我创建了两个php文件,文件名为page_login.php和login.php。 However when running login_post.php, the $json value unable pass to login.php 但是,在运行login_post.php时,$ json值无法传递给login.php

page_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: login.php中:

$json=json_decode($_REQUEST["json"],true);
print $json;

Try this 尝试这个

page_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,"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 的login.php

$json=json_decode($_REQUEST["json"],true);
foreach ($json as $key => $value) {
    echo $key . ": " . $value;
    echo "<BR>";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM