About receiving data from the ios side through the interface under the yii2 framework

WBOY
Release: 2016-08-31 08:54:55
Original
1428 people have browsed it

Use Yii::$app->request->post(); to receive data from ios, and printing the received data will show that the value is null. If you use $_data = empty($_POST) ? json_decode(file_get_contents('php://input'), TRUE) : ​​$_POST; you can get the value by receiving the data from ios, but as long as this data is not received, An error will be reported here. Please give me some guidance on the issue of data reception

Reply content:

Use Yii::$app->request->post(); to receive data from ios, and printing the received data will show that the value is null. If you use $_data = empty($_POST) ? json_decode(file_get_contents('php://input'), TRUE) : ​​$_POST; you can get the value by receiving the data from ios, but as long as this data is not received, An error will be reported here. Please give me some guidance on the issue of data reception

Check the HTTP request header of IOS There should be a problem with the request header settings This has not happened to our company’s ios children’s shoe request interface

It may be that the json format was not specified when iOS uploaded it.
You can print $_SERVER['CONTENT_TYPE'] to see if it is application/json.
If not, try it

<code class="php">$_SERVER['CONTENT_TYPE'] = 'application/json';
Yii::$app->request->post();</code>
Copy after login

This should be the request package sent by ios that directly packages the message body into a message in json format.
So the PHP side needs to use file_get_contents('php://input') to get the message body.

I used to make request headers for iOS and asked him if the request headers were set properly
Besides, the formal method of extracting json data should be like this
$body = Yii::$app->request->getRawBody();
json_decode($body,true);

If your API is developed based on the Yii2 framework, you can receive data in the following ways:

get request method:

<code>$value = Yii::$app->request->get("参数名");</code>
Copy after login

Post request method:

<code>$value = Yii::$app->request->post("参数名");</code>
Copy after login

If the Content-Type when the client transmits data is application/json, then you need to configure the following in the request section of the config.php file:

<code>'request' => [
    'cookieValidationKey' => '5opbkVM6PYmVxcyNvHG1wK06fkIh0vYG',
    'parsers' => [
        'application/json' => 'yii\web\JsonParser'
    ],
],</code>
Copy after login

In this way, you can successfully get the value passed in the form of json.
If the client serializes the object into json and delivers it, you can receive the object and save it to the database like this:

<code>$article = new Article();
$article->load(Yii::$app->getRequest()->getBodyParams(), '');
if (!$model->save()) {
    throw new ServerErrorHttpException('文章保存失败');
}</code>
Copy after login
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template