如何从 Guzzlehttp json 响应中删除不需要的字符
How to remove unwanted characters from Guzzlehttp json reposnce
您好,我是 Laravel 的新手,已经在 Guzzelhttp 上的 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));
没有任何运气我无法在 guzzle 文档页面上找到这个问题的任何答案保存说任何自定义 json_decode 选项应该在 getBody 中执行() 选项。
你需要做的
$body = substr($res->getBody(), 3)
而不是
$body = $res->getBody(substr($res, 3, strlen($res))
我最近在 Colin Viebrock 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 中大有作为,希望这对任何人如何 "Unable to parse response body into JSON: 4" 使用 Guzzle 有所帮助。
您好,我是 Laravel 的新手,已经在 Guzzelhttp 上的 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));
没有任何运气我无法在 guzzle 文档页面上找到这个问题的任何答案保存说任何自定义 json_decode 选项应该在 getBody 中执行() 选项。
你需要做的
$body = substr($res->getBody(), 3)
而不是
$body = $res->getBody(substr($res, 3, strlen($res))
我最近在 Colin Viebrock 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 中大有作为,希望这对任何人如何 "Unable to parse response body into JSON: 4" 使用 Guzzle 有所帮助。