当前位置: 首页 > 工具软件 > Goutte > 使用案例 >

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

龙学
2023-12-01

在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及其组件.

 类似资料: