首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从Guzzlehttp json存储库中删除不需要的字符

如何从Guzzlehttp json存储库中删除不需要的字符
EN

Stack Overflow用户
提问于 2015-08-12 21:09:39
回答 2查看 823关注 0票数 1

嗨,我是拉拉维尔的新手,在古泽尔的Goutte上尝试过几个图层,但我仍然无法弄清楚如何从json响应开始时删除3个不需要的字符,如这里使用curl和json_decode显示的那样。

代码语言:javascript
复制
$url = "URL to atom feed";
$user = "user";
$pass = "pass";

// using CURL to get our results
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, $user . ":" . $pass);
$output = curl_exec($ch);
curl_close($ch);

// decoding our results into an associative array
// doing a substring as there are 3 weird characters being passed back from IIS in front of the string
$data = json_decode(substr($output, 3, strlen($output)), true);

// grabbing our results object
$list = $data['$resources'];

我在我的ScrapeController

代码语言:javascript
复制
<?php

// app/controllers/ScrapeController.php
class ScrapeController extends BaseController {

    public function getIndex() {
        echo "Scrape index page.";
    }

    public function getNode($node) {
        echo "Scraped page $node";
    }

    public function getPages() {
        $client = new GuzzleHttp\Client();
        $res = $client->get('URL to atom feed', ['auth' =>  ['user', 'pass']]);
        echo $res->getStatusCode();
        // "200"
       // echo $res->getHeader('content-type');
        // 'application/json; charset=utf8'
       echo $res->getBody();
        // {"type":"User"...'

这就是我尝试过的$res->getBody(substr($res, 3, strlen($res));,没有任何运气,我在口吻文档页面上找不到解决这个问题的任何答案,除非说任何定制的json_decode选项都应该在getBody()选项中预先设置。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-08-12 22:09:11

你需要做的

代码语言:javascript
复制
$body = substr($res->getBody(), 3)

而不是

代码语言:javascript
复制
$body = $res->getBody(substr($res, 3, strlen($res))
票数 1
EN

Stack Overflow用户

发布于 2015-10-06 21:34:53

我最近在github上发现了由编写的代码,

代码语言:javascript
复制
$client = new Guzzle\Http\Client('http://example.com');

$client->addSubscriber( new Cviebrock\Guzzle\Plugin\StripBom\StripBomPlugin() );

$request = $client->get('some/request');

$response = $client->send($request);

$data = $response->json();

在laravel中工作,希望这能帮助任何人使用口香糖“无法将响应体解析为JSON: 4”。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31975462

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档