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'];
Related
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.
This question may be stupid. I want to know if its possible to send a string like {"type":"kissme", "who":"john bennet"} to rest controller as a post request.
I know that post data has to be sent as somekey=somevalue&otherkey=othervalue as body of the request. While receiving we refer to the key to get the data. But how can we send any string without assigning to a key like json data ? If it is possible to send, how to receive ?
Thank you,
Abbiya
REST implementation for CodeIgniter
You can use any of the following formats too simply by appending
/format/json to the end of your URL or passing the correct MIME-type:
xml
json
serialize
php (raw output that can be used in eval)
html
csv
Source: http://philsturgeon.co.uk/blog/2009/06/REST-implementation-for-CodeIgniter
i used file_get_contents('php://input'); while reading in controller. i use the content-type header as application/x-www-form-urlencoded while posting the data
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 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
The only way to send a JSON object is via JavaScript?
I'm developing a system that will have a lot of requests, send and receive Json.
If I can only work with Javascript to send the object, how do I trigger the js function?
Thanks in advance
[EDIT]
I have a C# working with database.
WCF making the communication between C# and PHP.
I was making the communication with WCF via SOAP, it was working fine but my boss want me to use JSON instead.
I've read some articles about JSON+PHP and the only way to send the JSON object is with JavaScript?
Once you have your JSON string encoded with json_encode() you simply echo it out with the correct MIME header.
header("Content-type: application/json");
echo json_encode($your_data);
to get the php file to return JSON you just make an array of the conditions and then json_encode echo it.....
response.php
$cond ? $response= array("conditions" => true) : $response = array("conditions"=>false);
echo json_encode($response);