嗨,我是拉拉维尔的新手,在古泽尔的Goutte上尝试过几个图层,但我仍然无法弄清楚如何从json响应开始时删除3个不需要的字符,如这里使用curl和json_decode显示的那样。
$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
<?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()选项中预先设置。
发布于 2015-08-12 22:09:11
你需要做的
$body = substr($res->getBody(), 3)而不是
$body = $res->getBody(substr($res, 3, strlen($res))发布于 2015-10-06 21:34:53
我最近在github上发现了由编写的代码,
$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”。
https://stackoverflow.com/questions/31975462
复制相似问题