I am trying to setup an automatic email handling. I am using external service https://www.email2json.net to convert emails to JSON/text format that is called on a webhook URL in my website.
How can I grab the raw post data and convert it to an object in PHP ?
Hence converting the email jebrish to something meaningful ?
Thanks.
You are getting a JSON object sent via POST, so you should first retrieve the input object with:
$input = file_get_contents("php://input");
Then you can easily handle this JSON object by using the json_decode native function to convert it into an associative array:
$decoded_input = json_decode($input, true);
Hope this helped.
Related
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];
Basic requirement is to know if we can iterate through $_POST when the request body payload has data sent in json format e.g.
{"City":{"countryCode": "IN","regionCode":"KR"}}
We are able to access this only if we send the data as
m={"City":{"countryCode": "IN","regionCode":"KR"}}
We are able to access this using $_POST['m']
The Content-Type is set to default application/x-www-form-urlencoded, when we set this as application/json $_POST is empty/null.
If we try to access this as $_POST instead of $_POST['m'] it returns null/empty.
NB: I am newbie to PHP. Is it possible to create webservices without any library. Without making use of any library can PHP accept the post request with json data.
To get raw POST data (as opposed to having to access individual POST variables such as $_POST['m']), you can use the following wrapper:
$json = file_get_contents('php://input');
You can read the manual on wrappers if you're interested in learning a bit more about them.
This is how i am getting data and saving it to my database.
$return = array();
$data = json_decode(stripslashes($_REQUEST['Data']));
$email = $data->{"paramA"};
$password = $data->{"paramB"};
i think it might help you.
I am using php to write out an html file, similar to a user profile, that contains a google map centered at the profile's address.
I know that the latitude/longitude is returned in latlng object format. Can I just parse this object out of the json using php, and pass it directly into the javascript, or is it not possible for php to pass this object? Furthermore, can this information be stored in an sql database (without using fusion table)?
edit: so if I had
$geoadd = $address.','.$city.','.$state.','.$zipcode.',USA';
$geoadd = urlencode($geoadd);
$url = "http://maps.googleapis.com/maps/api/geocode/output?address=".$geoadd;
$json_response = json_decode($url, TRUE);
I'd just pass $json_response into javascript?
You should make use of an ajax call and parse the gocoder responce in there.Just make use of jquery ajax call.
I am building an API using Codeigniter and Phils RESTserver and I am trying to send an array to the API using a POST request.
When I POST this:
query=("email#example.com","anna#nicole.com")
I can access it like this:
$this->post('query');
This produces:
("email#example.com","anna#nicole.com")
How can I loop through these email addresses in PHP?
Do anyone got another idea?
Thankful for all input!
If you are controlling the POST request, then you should POST an array to PHP, not a delimited list, which will automatically create an array that is available for use when the server receives the request. This is what J0HN means when he says use square brackets instead of parenthesis.
However, it depends on how you're sending the request to the server to determine the proper way to send an array to PHP. You might need:
query=["one#example.com", "two#example.com"]
query[]=["one#example.com", "two#example.com"]
Or other variants...
Since you don't elaborate on how you're making the request to the server, the proper way to POST an array cannot be determined.
On the other hand, if you can't POST an array for some reason, you can always send a JSON-encoded string and easily decode it:
// Assuming $this->post('query') = json_encode( array( "one#example.com", "two#example.com"));
$emails = json_decode( $this->post('query')); // Array of emails
I am building an API using Codeigniter and Phils RESTServer.
I want to send a bounch of email addresses to the API (best in JSON format).
How can I do this? How can I "receive" and "use" the JSON object that is sent to the server?
Right now I can send and receive parameters like this:
email=example#example.com
and I receive and use them like this:
$this->post('email')
I want to send and receive in this format instead
{"email":"example#example.com"}
How can I achieve this and how can I use the object?
UPDATE: I at least need to be able to send a normal array to the RESTserver.
Thankful for all input!
There is an output class in codeigniter. For json encode, you can use this:
$contents = $this->output
->set_content_type('application/json')
->set_output(json_encode(array('email' => 'example#example.com')));
echo $contents;//{"email":"example#example.com"}
How about posting the JSON string to the controller and then using json_decode() to extract the data out if it?
$data = json_decode($this->post('json'));
echo $data['email'];