0

I want to grab json string, set objects and httpheaders in 1.php (for example). Get these values in JSON format and display in next page (2.php).

I tried to use CURL method. I m very new to PHP. Not sure whether CURL method is the best to POST data.. Session or cookie method no need. Is there any other method other than this to POST data?

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:

How to post 1.php json value to 2.php.. either through CURL or other posting method.. Tried my best.. cant able to figure out the solution.

Thanks

TDG
  • 1,294
  • 4
  • 18
  • 50
  • 2
    Your answer was already solved here: http://stackoverflow.com/questions/6213509/send-json-post-using-php – Lotus Jan 23 '14 at 02:40
  • thanks.. Not sure what code to use to get Value from 1.php to 2.php.. even on 1.php, firefox debugger tool not shows any response value..CURL wont show any response? – TDG Jan 23 '14 at 03:28
  • Possible duplicate of [Send json post using php](https://stackoverflow.com/questions/6213509/send-json-post-using-php) – ToolmakerSteve Apr 10 '19 at 09:14

1 Answers1

3

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());
Louis.CLast
  • 424
  • 1
  • 3
  • 15
  • @venkatesha Sorry, echo $output, edited. In this case, you can't access directly to 2.php. – Louis.CLast Jan 23 '14 at 03:53
  • Thanks Louis.. Attached screenshot of output, http://teshdigitalgroup.com/1.JPG (1.php output), http://teshdigitalgroup.com/2.JPG (2.php output) – TDG Jan 23 '14 at 04:04
  • Thanks a lot for your quick replies. if i run in my xampp server below link http://localhost:1234/aws-sns/1.php .. i m getting "Object not found error!" whether i need to run this file in server host? – TDG Jan 23 '14 at 04:34
  • The line number of error ? I'm tested on PHP 5.4, windows, localhost. – Louis.CLast Jan 23 '14 at 04:37
  • sorry to trouble u again & again.. I uploaded your updated file to my linux server. Please check below links for output screen. Json data is not displaying in 2.php, whether i need to modify any code? http://teshdigitalgroup.com/new_1.JPG, http://teshdigitalgroup.com/new_2.JPG – TDG Jan 23 '14 at 04:41
  • Look at 1.php, find $request_url, echo it, or replace it with your full url to 2.php, view my updated answer. – Louis.CLast Jan 23 '14 at 05:28
  • Thanks.. already i changed my code as you mentioned. Need clarification for my last 2 items. 1) "'test' => 'data', 'new' => 'array'" missing in output. 2) want to add aws code "x-amz-sns-message-type: SubscriptionConfirmation" to header, i added already but it's not displaying in reponse/request header – TDG Jan 23 '14 at 05:58