sending request to server and getting response - php

i have question about sending and getting data from server, i just want to know is it doable what i want to achieve. So here is the thing:
i have some data that i want to send to server, like some id, name, surname
and i have server with ip 256.257.258.259, server waits for request with values encoded in url, so i can send data to server somehow like this:
$data = array(
'id'=>'c456ki98765',
'name'=>'john',
'surname'=>'smith');
$urlstring = http_build_query($data);
$url = 'http://256.257.258.259/folder/automatic?'.($urlstring);
i have a question there, should i use GET request to send this kind of data?
and would it look something like this?
$request = new HttpRequest('url', HttpRequest::METH_GET);
and when server gets my data it sends back response - for example json data - 'number' string or 'error' string if id is wrong. And i have another question there, how can i get data that server sends me after i have sent request?
how can i get that 'number'or 'error'? i hope i have made my question clear

You can use either a POST or a GET request depending on your requirements
Your $request is fine for setting up the request but then you need to send it:
$request->send();
and will need to do something with the response code:
$request->getResponseCode()
Hopefully the below links will come in handy (especially the bottom on in this case):
http://www.php.net/manual/en/function.http-get.php
http://www.php.net/manual/en/function.http-post-data.php
http://www.php.net/manual/en/httprequest.send.php

Related

Display JSON data from POST request in PHP

I have an apache2 server and php file to receive json via POST in my php code like this
$json = json_decode(file_get_contents('php://input'), true);
$mac_ = $json["code"];
$userId_ = $json["msg"];
and saving it to mysql. This work when I send post via python's requests but not when I'm recieving data from API android device. - When I look at the apache log file, there is a connection, so I'm assuming that some data are received, but because I can't touch that API android device I'm not sure if this part $json["code"] has a right parameter. I would like to know a way to save somewhere all recieved data from this part
$json = json_decode(file_get_contents('php://input'), true);
because I can't check it by echo or so

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/

PHP Unirest plugin not working properly

I use Unirest for PHP to do some HTTP Requests. Everything works fine, until I want to pass a fairly complex JSON to my Node.js router.
First I do a GET request that returns a JSON Object, then I extend this JSON object (needs to be done, I know it sucks) and want to feed it back into my other http POST request... and here is where the trouble starts:
I echoed the JSON that is returned and copied the output into postman -> works fine. If I want use the JSON directly in PHP in next request:
$teamsMemberOf is the variable which is containing the GET response.
$headers = array("Accept" => "application/json");
$newBody = '{"team":'.$teamsMemberOf->raw_body.'}';
$relevantBoxesAmount = Unirest\Request::post("http://localhost:3001/my/route/".$result['_id']."/get-something-from-server", $headers, $newBody);
and it doesn't work.
Error is 500 and 'Cannot read property '0' of undefined' which is certainly related to something in the JSON object.
Does someone have an idea how to fix it?

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

How to access Backbone model ID from a fetch request on server side

In an attempt to learn how BackboneJS works, I am building a PHP script with basic CRUD cabibilities. My problem is, when BackboneJS sends a "Fetch" request (GET) it will send an ID encoded in JSON. My problem is, how can I handle this on the server?
I have tried:
$data = json_decode(file_get_contents('php://input'));
or just handling it via the $_GET array.
I simply need to get the ID that comes through, so I can do some DB work with it.
You get the ID from _SERVER["REQUEST_URI"]
var Student = Backbone.Model.extend({
urlRoot : "/students"
});
var student = new Student({id:123});
student.fetch();
url that is hit is www.yourdomain.com/students/123
Hence you need to parse it out from the request uri.
In a model.fetch() ID is not passed as Request payload, but as part of the url it self.
ID along with all other attributes are passed as request payload for model.put(). model.save() does not have any id, attributes are passed as request payload in which case
you need to
$content = json_decode(file_get_contents('php://input'));

Categories