我有一个php文件(sample1.php),它调用另一个php文件(sample2.php) 通过标题功能。
{ "_id" : ObjectId("5922846dbcf60428d0f69f6e"), "a" : [ 1, 2 ] }
{ "_id" : ObjectId("5922847cbcf60428d0f69f6f"), "a" : [ ] }
如何将JSON对象从一个php传递到另一个php 我需要POST JSON对象,以便第二个文件可以使用
访问它 header("Location: "sample2.php");
答案 0 :(得分:0)
您可以使用cURL方法
<?php
//set POST variables
$url = 'sample2.php';
$fields = array(
'json' => urlencode($json/*your json that want to pass*/),
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
?>
或者您可以参考此link:)
答案 1 :(得分:0)
如果你必须使用header()继续sample2.php,那么在sample2.php中你不能使用$ _POST来获取数据。
将它放入会话存储(在sample1.php中):
$_SESSION['jsonData'] = $yourJsonData;
然后到达sample2.php:
if( isset($_SESSION["jsonData"]) ) { ... }
不要忘记初始化会话等。请参阅http://php.net/manual/en/session.examples.basic.php