Unable to get data Posted by webhook in JSON String - php

I am using web hook for get user details from our company's signup page.
Data posted from signup page in JSON format. Posted JSON String is below
{"FirstName":"dharampal","EmailAddress":"er.dpyadav#gmail.com","Mobile":"123456789","Company":"Instasafe","LandingPageURL":"http://instasafe.com/SignUp","LandingPageId":"11e2-b071-123141050daa"}
In receiving end, we are using PHP codeIgNiter, I tried below methods but unable to get data.
$postedData = $_POST;
print_r($postedData);
it gives : empty array
other method i tried is :
$postedData = json_decode(file_get_contents('php://input'), TRUE);
print_r($postedData);
it gives : NULL

Seems like the problem is a typo with $ostedData and $postedData
$ostedData = json_decode(file_get_contents('php://input'), TRUE);
print_r($postedData);

Related

Can't get POST data from an API callback

I am trying to get the POST data that is being sent to a controller method within my CI project. However, I am not able to get this data which ever way I try to get it.
I have checked NGROK Inspect and Im able to see the data that I want as you can see in the screenshot below:
The problem is when I try to access this date from the controller method itself, I get nothing, not sure if Im accessing it wrong, but the following are what I have tried:
$Data = json_decode(file_get_contents('php://input'), true); //Method 1
$response = $this->input->post("Body"); // Method 2
$data = $this->input->post(); //Method 3
I get nothing whenever I try to use the methods above.
This is how the Controller method looks like, basically, it creates a json file and saves the response there:
public function resolve_payment(){
$data = $this->input->post();
$fp = fopen("testdir.json", 'w');
fwrite($fp, json_encode($data, JSON_UNESCAPED_SLASHES));
fclose($fp);
}

Get and Send file with PHP and RESTful API

One of my partners has an API service which should send an HTTP POST request whenever a new file is published. This requires me to have an api file which will get the POST this way:
http://myip:port/api/ReciveFile
and requesting that the JSON format request should be:
{
"FILE ":"filename.zip",
"FILE_ID":"123",
"FILE_DESC":"PRUPOUS_FILE",
"EXTRAVAR":"",
"EXTRAVAR2":"",
"USERID":" xxxxxxxxxxxx",
"PASSWORD":"yyyyyyyyyyy"
}
Meanwhile it should issue a response, in JSON format if it got the file or not
{"RESULT_CODE":0,"RESULT_DESCR":""}
{"RESULT_CODE":1001,"RESULT_DESCR":"Bad request"}
And after, when I am finished elaborating the file, I should send back the modified file same way.
The question is, now basically from what I understand he will send me the variables witch I have to read, download the file, and send a response back.
I am not really sure how to do this any sample code would be welcomed!
I'm not sure exactly what the question is, but if you mean creating a success response in JSON for if an action occurred while adding data to it which is what can be understood from the question, just create an array with the values you wish to send back to the provider and do json_encode on the array which should create json and just print it back as a response.
About receiving the information; all you have to do is use the integrated curl functions or use a wrapper (Guzzle, etc) to output the raw JSON or json_decode data into a variable and do whatever you please with it.
From what I read in the question, you wish to modify the contents of it. This can be achieved by just decoding the json and changing the variables in the array, then printing the modified JSON back as a response.
Example (this uses GuzzleHTTP as an example):
$res = $client->request('GET', 'url');
$json = $res->getBody();
$array = json_decode($json, 1); // decode the json
// Start modifying the values or adding
$array['value_to_modify'] = $whatever;
$filename = $array['filename']; // get filename
// make a new request
$res = $client->request('GET', 'url/'.$filename);
// get the body of specified filename
$body = $res->getBody();
// Return the array.
echo json_encode($body);
References:
http://docs.guzzlephp.org/en/latest/

How to read GET and POST request in PHP (post is in json)

I have a javascript (not from me) to sync information between client (javascript) and server (php) and javascript send to server GET and POST information, I can read easy GET information so I try to read $_POST information and cannot retrieve POST information.
I try print_r($_POST) and just returns Array() with nothing.
I try to look it with Firebug and Console into Firefox and I see that
How can I retrieve and treat json string into >Transmission de la requete< into my PHP code? I can easily retrieve GET parameters such as token, src and etc so I can't retrieve POST transmission.
Thank you for your helping !
I don't really get you. But since it's something like JSON request sent to the page. You can use the file_get_contents('php://input') to grab any JSON request sent. Then you can decode it into an object or an array.
Example
$request = file_get_contents('php://input');
To obtain an object
$input = json_decode($request);
So Input fields will be
$input->name; $input->password;
To Obtain an array
$input = json_decode($request, true);
So Input fields will be
$input[name]; $input[password];

Retrieve JSON Object from REST Blogger Api Request with PHP

I'm still wondering how to get json object from REST Api request from this link https://developers.google.com/blogger/docs/3.0/using.
It says GET https://www.googleapis.com/blogger/v3/blogs/2399953?key=YOUR-API-KEY request will give informations about particular blog.
I'm also PHP noobs. Here's my code.
<?php
$blogID = 'xxx';
$apikey = 'yyy';
$requestURL = "https://www.googleapis.com/blogger/v3/blogs/{$blogID}?key={$apikey}";
$json = file_get_contents($requestURL);
print_r($json);
?>
I have no idea about this, so please help me to find where the problem is. Thanks in advance
Try this:
$json = json_decode(file_get_contents($requestURL));
You are fetching a JSON string and trying to print_r it, which only works on arrays.
json_decode, as its name implies, decodes the JSON string and transforms it into an array / object.

Accessing posted Json data in php script

I'm using the Requests for PHP library to POST some json data to another PHP script (I'm using Laravel's Response::json method to generate the json output:
public function postIndex()
{
$input = Input::get();
$data = Response::json($input);
$url = 'http://mydomain.com/emails/events';
$response = Requests::post($url, array('Content-Type' => 'application/json'), $data);
return $response->status_code;
}
I need the script on the receiving end (http://mydomain.com/emails/events) to decode and process the json, but I'm having a hard time accessing it. I setup a simple test script that emails me the contents of $_POST, but it comes up empty every time.
$post_data = print_r($_POST,true);
mail("my#email.com","post data",$post_data);
What am I doing wrong here?
PHP do not parse Json POST. So you need to get raw post data like this:
$data = file_get_contents("php://input");
Info about php php wrappers

Categories