使用 CURL 通过 JSON 传递变量

如何通过 JSON 将表单变量传递到另一个页面?我创建了两个 php 文件,文件名为 page_login.php 和 login.php。但是在运行 login_post.php 时,$json 值无法传递给 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);

登录.php:


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

print $json;


尚方宝剑之说
浏览 260回答 1
1回答

慕哥6287543

试试这个page_login.php$email = "test@gmail.com";$password = "123";$json = array('email' => $email, 'password' => hash("sha256",$password));$json = json_encode($json);$ch = curl_init();&nbsp; &nbsp; &nbsp;&nbsp;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)));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);$server_output = curl_exec($ch);curl_close ($ch);echo $server_output;登录.php$json=json_decode($_REQUEST["json"],true);foreach ($json as $key => $value) {&nbsp; &nbsp; echo $key . ": " . $value;&nbsp; &nbsp; echo "<BR>";}
打开App,查看更多内容
随时随地看视频慕课网APP