Get json data in Bitrix mobile app?

Hello!

Need to receive orders from the mobile app and add them to the site Bitrix.
New orders come with a mobile app POST as json:

{
 "version": "1.4.1",
 "method": "neworder",
 "platform": "android",
 "appId": 112065,
 "user": "user",
 "data": {
 "deliveryType": "contact operator",
 "deliveryData": [],
 "items": [
{
 "count": 1,
 "id": "577094"
}
],
 "fake": 0,
 "order_info": {
 "name": "test",
 "phone": "999 999-99-99",
 "email": "test@test.ru",
 "comments": null,
 "pointId": null,
 "regionId": "rest_267874043",
 "payment_type": {
 "id": "158ad92727wdew",
 "title": "Cash",
 "type": "cash"
}
},
 "payment": {
 "type": "offline",
 "transactionId": null
}
},
 "region_data": {
 "full_title": "Moscow, Moscow, Russia",
 "google_place_id": "ChIJybDUc_xKtUYRTM9XV8zWRD0",
 "id": "rest_267874043",
 "is_selected": true,
 "title": "Moscow"
},
 "access_token": null
};


How on Bitrix to obtain these data?
Added script getOrder.php in the root of the site Bitrix:

$postData = file_get_contents('php://input'); // catch the data
$data = json_decode($postData, true);

$file = 'order.txt';
$current = file_get_contents($file);
$current = $postData;
file_put_contents($file, $current); // write to the file


I want to test the script at all, so from another site, ajax send json data (example above) and it works:
POST http://--------------/getOrder.php 405 (Not Allowed)

Failed to load http://---------/getOrder.php: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://---------' is therefore not allowed access. The response had HTTP status code 405.

Understand that there is no access? Adding a title
header('Access-Control-Allow-Origin: *') in the receiving script does not change anything.

What are you doing wrong?
June 14th 19 at 20:33
1 answer
June 14th 19 at 20:35
The web browser on this url?

*for debugging such things is a great tool Restlet Client
Yes, opens. - Catalina commented on June 14th 19 at 20:38
,
so with another site
meeeoooow, how about you send crossdomain requests? - reilly commented on June 14th 19 at 20:41
,

$.ajax({
 type: "POST",
 url: 'url',
data:data,
 dataType: 'jsonp',
 error: function(request,error) {
console.log(request);
console.log(error);
},
 success: function(data) {
console.log(data);

}
 });


Corrected, before it was sent not via jsonp.

Now a 405 error no, but there was an error while parsing
"Uncaught SyntaxError: Unexpected token : "

In response to the send:
echo json_encode(
array(
 'status' => 1,
 'message' => 'Error'

)
 );


Swears on the first colon
{"status":1,"message":"Error"}

Maybe something else need to send back? - Catalina commented on June 14th 19 at 20:44
,
  1. add header('Content-Type: application/json');
  2. Try this:
    echo json_encode(
    array(
     'status' => '1',
     'message' => 'Error'
    )
     );

- reilly commented on June 14th 19 at 20:47
that worked,
but now appeared the error "500 Internal Server Error"

header('Content-Type: application/json') added, but I see:
59d396e36946b894216115.png

The complete code of the receiving script:

<?php 
header('Content-Type: application/json');

$postData = file_get_contents('php://input');
$data = json_decode($postData, true);

$file = 'order.txt';
$current = file_get_contents($file);

$current = $postData;
file_put_contents($file, $current);


echo json_encode(
array(
 'status' =--> '1',
 'message' => 'Error'

 );


It turns out the error on the server side, so something in the file itself wrong? - Catalina commented on June 14th 19 at 20:50
500 Internal Server Error
Where do you catch this error? using jsonp? Debug through the Restlet Client.

Here it is:
$file = 'order.txt';
$current = file_get_contents($file);

$current = $postData;
file_put_contents($file, $current);

it why? - reilly commented on June 14th 19 at 20:53
to verify the obtained data is recorded in the file.
Look in the browser of the website from which you send the data, there and you see this error.
Using Restlet Client, the error is the same. - Catalina commented on June 14th 19 at 20:56
500 Internal Server Error
And admin open? 500ya crashes are usually due to incorrect configuration of Apache.

If you just open in browser http://--------------/getOrder.php the 500 error is? - reilly commented on June 14th 19 at 20:59
now doesn't open, the error remained.
Although this all opened, the settings have not changed on the website.
The admin panel of the website opens. - Catalina commented on June 14th 19 at 21:02
,
dataType: 'jsonp',

JSONP != JSON

Try this:

On js side:
$.ajax({
 type: "POST",
 url: '/testOrder.php',
 data: {id:1},
 dataType: 'json',
 error: function(request,error) {
console.log(request);
console.log(error);
},
 success: function(data) {
 console.log(data); 
}
});


File testOrder.php:

<?php

$id = $_REQUEST['id'];
echo json_encode(['new_id'=-->$id]);


In response, you should return an object with a key new_id and its value should be 1 (that I sent)

If it will work right, the basic algorithms worked and the problem is in your code.
If you look at the error "500 Internal Server Error" - so somewhere in your code on the php side error.

If You see an error with origin, it will not help You setting the header Access-Control-Allow-Origin in your php code, as it is not, he gives it. - Roel.OConnell30 commented on June 14th 19 at 21:05

Find more questions by tags 1C-Bitrix