日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > php >内容正文

php

php 接收curl json数据格式,curl发送 JSON格式POST数据的接收,以及在yii2框架中的实现原理【精细剖析】...

發(fā)布時(shí)間:2025/3/21 php 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php 接收curl json数据格式,curl发送 JSON格式POST数据的接收,以及在yii2框架中的实现原理【精细剖析】... 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1.通過(guò)curl發(fā)送json格式的數(shù)據(jù),譬如代碼:

function http_post_json($url, $jsonStr)

{

$ch = curl_init();

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_HTTPHEADER, array(

'Content-Type: application/json; charset=utf-8',

'Content-Length: ' . strlen($jsonStr)

)

);

$response = curl_exec($ch);

//$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

curl_close($ch);

return $response;

}

$api_url = 'http://fecshop.appapi.fancyecommerce.com/44.php';

$post_data = [

'username' => 'terry',

'password' => 'terry4321'

];

然后在接收端,使用$_POST接收,發(fā)現(xiàn)打印是空的

原因是,PHP默認(rèn)只識(shí)別application/x-www.form-urlencoded標(biāo)準(zhǔn)的數(shù)據(jù)類型,因此接收不到,只能通過(guò)

//第一種方法

$post = $GLOBALS['HTTP_RAW_POST_DATA'];

//第二種方法

$post = file_get_contents("php://input");

來(lái)接收

2.如果我們?cè)赮ii2框架內(nèi),想通過(guò)

$username = Yii::$app->request->post('username');

$password = Yii::$app->request->post('password');

這種方式獲取第一部分使用curl json方式傳遞的post參數(shù),我們發(fā)現(xiàn)是不行的,我們需要設(shè)置yii2 request component

'request' => [

'class' => 'yii\web\Request',

'parsers' => [

'application/json' => 'yii\web\JsonParser',

],

],

然后我們通過(guò)

$username = Yii::$app->request->post('username');

$password = Yii::$app->request->post('password');

發(fā)現(xiàn)是可以取值的了,然后如果你打印 $_POST,會(huì)發(fā)現(xiàn)這里依舊沒(méi)有值,這是為什么呢?

下面我們通過(guò)代碼順藤摸瓜的查一下Yii2的源代碼:

1.打開(kāi) yii\web\Request 找到post()方法:

public function post($name = null, $defaultValue = null)

{

if ($name === null) {

return $this->getBodyParams();

}

return $this->getBodyParam($name, $defaultValue);

}

發(fā)現(xiàn)值是由 $this->getBodyParam($name, $defaultValue) 給予

然后找到這個(gè)方法,代碼如下:

/**

* Returns the request parameters given in the request body.

*

* Request parameters are determined using the parsers configured in [[parsers]] property.

* If no parsers are configured for the current [[contentType]] it uses the PHP function `mb_parse_str()`

* to parse the [[rawBody|request body]].

* @return array the request parameters given in the request body.

* @throws \yii\base\InvalidConfigException if a registered parser does not implement the [[RequestParserInterface]].

* @see getMethod()

* @see getBodyParam()

* @see setBodyParams()

*/

public function getBodyParams()

{

if ($this->_bodyParams === null) {

if (isset($_POST[$this->methodParam])) {

$this->_bodyParams = $_POST;

unset($this->_bodyParams[$this->methodParam]);

return $this->_bodyParams;

}

$rawContentType = $this->getContentType();

if (($pos = strpos($rawContentType, ';')) !== false) {

// e.g. application/json; charset=UTF-8

$contentType = substr($rawContentType, 0, $pos);

} else {

$contentType = $rawContentType;

}

if (isset($this->parsers[$contentType])) {

$parser = Yii::createObject($this->parsers[$contentType]);

if (!($parser instanceof RequestParserInterface)) {

throw new InvalidConfigException("The '$contentType' request parser is invalid. It must implement the yii\\web\\RequestParserInterface.");

}

$this->_bodyParams = $parser->parse($this->getRawBody(), $rawContentType);

} elseif (isset($this->parsers['*'])) {

$parser = Yii::createObject($this->parsers['*']);

if (!($parser instanceof RequestParserInterface)) {

throw new InvalidConfigException("The fallback request parser is invalid. It must implement the yii\\web\\RequestParserInterface.");

}

$this->_bodyParams = $parser->parse($this->getRawBody(), $rawContentType);

} elseif ($this->getMethod() === 'POST') {

// PHP has already parsed the body so we have all params in $_POST

$this->_bodyParams = $_POST;

} else {

$this->_bodyParams = [];

mb_parse_str($this->getRawBody(), $this->_bodyParams);

}

}

return $this->_bodyParams;

}

打印 $rawContentType = $this->getContentType(); 這個(gè)變量,發(fā)現(xiàn)他的值為:

application/json , 然后查看函數(shù)getContentType()

public function getContentType()

{

if (isset($_SERVER['CONTENT_TYPE'])) {

return $_SERVER['CONTENT_TYPE'];

}

if (isset($_SERVER['HTTP_CONTENT_TYPE'])) {

//fix bug https://bugs.php.net/bug.php?id=66606

return $_SERVER['HTTP_CONTENT_TYPE'];

}

return null;

}

也就是 當(dāng)我們發(fā)送json格式的curl請(qǐng)求, $_SERVER['CONTENT_TYPE'] 的值為 application/json

2.重新回到上面的函數(shù) getBodyParams(),他會(huì)繼續(xù)執(zhí)行下面的代碼:

if (isset($this->parsers[$contentType])) {

$parser = Yii::createObject($this->parsers[$contentType]);

if (!($parser instanceof RequestParserInterface)) {

throw new InvalidConfigException("The '$contentType' request parser is invalid. It must implement the yii\\web\\RequestParserInterface.");

}

$this->_bodyParams = $parser->parse($this->getRawBody(), $rawContentType);

}

$parser 就是根據(jù)我們下面的request component配置中的 parsers中得到'yii\web\JsonParser',進(jìn)而通過(guò)容器生成出來(lái)的

'request' => [

'class' => 'yii\web\Request',

'enableCookieValidation' => false,

'parsers' => [

'application/json' => 'yii\web\JsonParser',

],

],

因此返回值就是 $parser->parse($this->getRawBody(), $rawContentType); 返回的,

3.首先我們查看傳遞的第一個(gè)參數(shù)是函數(shù) $this->getRawBody(),代碼如下:

public function getRawBody()

{

if ($this->_rawBody === null) {

$this->_rawBody = file_get_contents('php://input');

}

return $this->_rawBody;

}

通過(guò)這個(gè)函數(shù),回到前面我們說(shuō)的,可以通過(guò)

//第一種方法

$post = $GLOBALS['HTTP_RAW_POST_DATA'];

//第二種方法

$post = file_get_contents("php://input");

這兩種方式獲取curl json傳遞的json數(shù)據(jù),yii2使用的是第二種。

然后我們打開(kāi)yii\web\JsonParser

/**

* Parses a HTTP request body.

* @param string $rawBody the raw HTTP request body.

* @param string $contentType the content type specified for the request body.

* @return array parameters parsed from the request body

* @throws BadRequestHttpException if the body contains invalid json and [[throwException]] is `true`.

*/

public function parse($rawBody, $contentType)

{

try {

$parameters = Json::decode($rawBody, $this->asArray);

return $parameters === null ? [] : $parameters;

} catch (InvalidParamException $e) {

if ($this->throwException) {

throw new BadRequestHttpException('Invalid JSON data in request body: ' . $e->getMessage());

}

return [];

}

}

可以看到這里是將傳遞的json轉(zhuǎn)換成數(shù)組,然后Yii::request->post('username')就可以從返回的這個(gè)數(shù)組中取值了

總結(jié):

1.在Yii2框架中要用封裝的post() 和 get()方法, 而不要使用$_POST $_GET等方法,因?yàn)閮烧呤遣幌嗟鹊摹?/p>

2.Yii2做api的時(shí)候,如果是json格式傳遞數(shù)據(jù),一定不要忘記在request component中加上配置:

'request' => [

'class' => 'yii\web\Request',

'parsers' => [

'application/json' => 'yii\web\JsonParser',

],

],

本文由 Terry 創(chuàng)作,采用 知識(shí)共享署名 3.0 中國(guó)大陸許可協(xié)議 進(jìn)行許可。

可自由轉(zhuǎn)載、引用,但需署名作者且注明文章出處。

總結(jié)

以上是生活随笔為你收集整理的php 接收curl json数据格式,curl发送 JSON格式POST数据的接收,以及在yii2框架中的实现原理【精细剖析】...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 日韩成人中文字幕 | 国产偷自拍视频 | 免费看a网站 | 国产麻豆网| 黄色影音| 高清日韩 | 日韩不卡视频一区二区 | 久久久久久福利 | 国产一线天粉嫩馒头极品av | 不卡中文字幕在线 | 97在线播放免费观看 | 91在线不卡 | 国产女人叫床高潮大片免费 | 九热视频在线观看 | 91看片在线 | 亚洲欧洲无码一区二区三区 | 99久99| www.亚洲| 国产精品人人爽人人爽 | 国产高清sp | 四川黄色一级片 | 麻豆精品 | 蜜臂av| 污污的视频在线观看 | 日本欧美激情 | 欧美自拍视频 | 亚洲精品视频91 | 亚洲国产福利 | 国产毛片精品国产一区二区三区 | 欧美日韩99 | 中文字幕av专区 | 岛国精品在线播放 | 日韩欧美视频在线 | 久久久久久久久久久国产 | 卡通动漫精品一区二区三区 | 亚洲综合中文 | 国产在线观看你懂的 | 中文字幕一区二区三区波野结 | 91官网在线| 国产大奶在线观看 | 一本一道久久a久久精品综合 | 一二三区不卡 | 日本护士体内she精2xxx | 天天色天天爽 | 欧美 日本 国产 | 一区视频免费观看 | 91久久人澡人人添人人爽欧美 | 久久久久久久久久久电影 | 成人黄色激情小说 | 国产精品久久久久久久一区二区 | 国产精品一区二区在线观看 | 在线黄色免费网站 | 91国在线视频 | 制服丝袜在线视频 | 美女视频黄a视频全免费观看 | 国产成人资源 | 强行挺进白丝老师翘臀网站 | 狼性av懂色av禁果av | 免费在线观看不卡av | av在线黄色| 四虎在线影视 | www在线免费观看 | 黄网站在线观 | 欧美一区二区最爽乱淫视频免费看 | 91最新入口 | 黄色应用在线观看 | 毛片内射 | 国产毛片视频网站 | 伊人久久精品一区二区三区 | 一区二区三区四区不卡 | 手机看片福利一区 | 瑟瑟视频免费看 | 成人欧美一区二区三区在线播放 | 天天狠天天透 | 香蕉污视频 | 色悠悠av| 玖玖免费 | 欧美视频在线免费 | 特级做a爰片毛片免费69 | www三级免费 | 一女双乳被两男吸视频 | 99视频这里有精品 | 男女作爱网站 | 美腿丝袜亚洲色图 | 成年人在线观看视频免费 | 91成年影院| 人人射影院| 日本一区电影 | 最新激情网 | 男人视频网 | 五月天婷婷综合 | 女同亚洲精品一区二区三 | 美国av大片 | 亚洲免费av电影 | 高h校园不许穿内裤h调教 | 亚洲精品一区二三区不卡 | 91精品国产色综合久久不卡粉嫩 | 国产精品99久久久久久宅男 | 公车乳尖揉捏酥软呻吟 |