Codeigniter and PHP. Using POST to access array - php

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

Related

Passing JSON in GET request

I search SO for similar questions, but even though the title was same, I was unable to find an answer so I am reposting.
I have a REST endpoint which till now was a POST call even though it was only retrieving data. I am trying to convert this to a GET call. The payload is a JSON for the POST method. I know I can convert the JSON into URL parameters and pass it. However I wanted to check if I can pass this JSON as a GET request itself.
Is there any way in PHP to pass JSON in a GET request?
It's a messy proposition but you should be able to urlencode your JSON and pass it that way
$data = ['some' => 'value'];
echo urlencode(json_encode($data));

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];

JSON array as value getting text

I have created API for creating a customer in PHP, the client side is in Android would use a POST request to access this API. For testing I have used chrome postman where I had used post data as :
{"name":"test","marks":["12","13","16"],"age":19}
and it is working fine.
I am getting marks as array itself ["12","13","16"].
But from android client I am getting as text ["12","13","14"].
What may be the possible reason?
POST it as :
{"name":"test","marks":[12,13,16],"age":19}
You are posting it as String inside array ["12","13","16"]
try to use it like
{"name":"test","marks":["12","13","16"],"age":19}

Is it possible to create a Restful webservice with POST requests in PHP without using any library?

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.

(website data exchange || communication between sites) via php

Is there any way to send simple string
from websiteA.com -to-> websiteB.com with php?
Where websiteB.com "listens"(as not knowing the existence of websiteA.com) any incoming simple strings sent and receives it?
Setup a script on websiteB that handles a POST request (e.g. a string).
Use cURL or sockets on websiteA to send POST data (e.g. a string) to websiteB
Very very simple vay is to use "rest api" - eg. create PHP file, which will recieve string and process it , and return XML / JSON ... or other formatted ( or unformatted ) answer. Client site ( eg. A ) just need to fopen ( or send request using cURL ) this php file with approtiate string
More complex way is to use SOAP - it have standart object in PHP , but it is reccomed to write wsdl file, which is a bit complex. see google for details

Categories