php 解析json 可以吗,php – 可以用Goutte解析JSON吗?

在Goutte图书馆深入搜索后,我发现了一种方式,我想分享.因为Goutte是非常强大的库,但是文档非常复杂.

通过(Goutte> Guzzle)解析JSON

只需获取所需的输出页面并将json存储到数组中.

$client = new Client(); // Goutte Client

$request = $client->getClient()->createRequest('GET', 'http://***.json');

/* getClient() for taking Guzzle Client */

$response = $request->send(); // Send created request to server

$data = $response->json(); // Returns PHP Array

通过(Goutte Guzzle)解析带有Cookie的JSON – 用于身份验证

发送请求之一的网站页面(主页看起来更好)获取cookie,然后使用这些cookie进行身份验证.

$client = new Client(); // Goutte Client

$crawler = $client->request("GET", "http://foo.bar");

/* Send request directly and get whole data. It includes cookies from server and

it automatically stored in Goutte Client object */

$request = $client->getClient()->createRequest('GET', 'http://foo.bar/baz.json');

/* getClient() for taking Guzzle Client */

$cookies = $client->getRequest()->getCookies();

foreach ($cookies as $key => $value) {

$request->addCookie($key, $value);

}

/* Get cookies from Goutte Client and add to cookies in Guzzle request */

$response = $request->send(); // Send created request to server

$data = $response->json(); // Returns PHP Array

我希望它有所帮助.因为我几乎花了3天时间来了解Gouttle及其组件.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值