【问题标题】:PHP Post JSON value to next pagePHP 将 JSON 值发布到下一页
【发布时间】:2014-02-13 10:00:10
【问题描述】:

我想在 1.php 中获取 json 字符串、设置对象和 httpheaders(例如)。以 JSON 格式获取这些值并显示在下一页 (2.php) 中。

我尝试使用 CURL 方法。我对 PHP 很陌生。不确定 CURL 方法是否是 POST 数据的最佳方法。不需要 Session 或 cookie 方法。除此以外还有其他方法可以 POST 数据吗?

1.PHP:

<?php
$data = array($item_type = "SubscriptionConfirmation";
$message = "165545c9-2a5c-472c-8df2-7ff2be2b3b1b";
$Token = "2336412f37fb687f5d51e6e241d09c805a5a57b30d712f794cc5f6a988666d92768dd60a747ba6f3beb71854e285d6ad02428b09ceece29417f1f02d6";
$TopicArn = "arn:aws:sns:us-east-1:123456789012:MyTopic";
$Message = "You have chosen to subscribe to the topic arn:aws:sns:us-east-1:123456789012:MyTopic.\nTo confirm ";
$SubscribeURL = "https://sns.us-east-1.amazonaws.com/?Action=ConfirmSubscription&TopicArn=arn:aws:sns:us-east-1:123456789012:MyTopic&Token=2336412f37fb6";
$Timestamp = "2012-04-26T20:45:04.751Z";
$SignatureVersion = "1";
$Signature = "EXAMPLEpH+DcEwjAPg8O9mY8dReBSwksfg2S7WKQcikcNKWLQjwu6A4VbeS0QHVCkhRS7fUQvi2egU3N858fiTDN6bkkOxYDVrY0Ad8L10Hs3zH81mtnPk5uvvolIC1CXGu43obcgFxeL3khZl8IKvO61GWB6jI9b5+gLPoBc1Q=";
$SigningCertURL = "https://sns.us-east-1.amazonaws.com/SimpleNotificationService-f3ecfb7224c7233fe7bb5f59f96de52f.pem";);                                                
$json_data = json_encode($data);                                                                            
$ch = curl_init('http://example.com');                                                     
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);                                                               
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
curl_setopt($ch, CURLOPT_CAINFO, "/path/to/CA.crt");                                                                 
curl_setopt($ch, CURLOPT_HTTPHEADER,array(                                                                 
'Content-Type: application/json',                                                                       
'Content-Length: ' . strlen($json_data))                                                                      
);
$output = curl_exec($ch);
$result = json_decode($output);
echo $result;
?>

2.PHP:

如何通过 CURL 或其他发布方法将 1.php json 值发布到 2.php.. 尽我所能.. 无法找出解决方案。

谢谢

【问题讨论】:

标签: php json curl


【解决方案1】:

1.php

$data = array(
    'test' => 'data',
    'new'  => 'array');
$json_data = json_encode($data);    

// Only work with your example, curl need full url
$request_url = 'http://teshdigitalgroup.com/2.php';      

$ch = curl_init($request_url);                                                     
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);                                                              
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($json_data))                                                                       
);     
$output = curl_exec($ch);
print_r($output);

AND 2.php

print_r(file_get_contents('php://input'));

// Response 1.php's request HTTP Header
function parseRequestHeaders() {
    $headers = array();
    foreach($_SERVER as $key => $value) {
        if (substr($key, 0, 5) <> 'HTTP_') {
            continue;
        }
        $header = str_replace(' ', '-', ucwords(str_replace('_', ' ', strtolower(substr($key, 5)))));
        $headers[$header] = $value;
    }
    return $headers;
}

print_r(parseRequestHeaders());

【讨论】:

  • @venkatesha 抱歉,回显 $output,已编辑。这种情况下不能直接访问2.php。
  • 感谢 Louis.. 附上输出截图,teshdigitalgroup.com/1.JPG(1.php 输出),teshdigitalgroup.com/2.JPG(2.php 输出)
  • 非常感谢您的快速回复。如果我在链接 localhost:1234/aws-sns/1.php 下方的 xampp 服务器中运行 .. 我得到“找不到对象错误!”我是否需要在服务器主机上运行这个文件?
  • 错误的行号?我在 PHP 5.4、windows、localhost 上进行了测试。
  • 很抱歉再次给您带来麻烦。我将您更新的文件上传到我的 linux 服务器。请检查以下链接以获取输出屏幕。 2.php中没有显示json数据,是否需要修改任何代码? teshdigitalgroup.com/new_1.JPG, teshdigitalgroup.com/new_2.JPG
猜你喜欢
  • 2017-03-07
  • 2017-10-12
  • 2023-03-13
  • 2015-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多