【发布时间】:2015-11-05 16:01:04
【问题描述】:
您好,我是 Laravel 的新手,并且在 Guzzelhttp 上尝试了几个关于 Goutte 的教程,但我仍然无法弄清楚如何使用 curl 和 json_decode 从 json 响应的开头删除 3 个不需要的字符。
$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() 选项中执行。
【问题讨论】:
标签: php json laravel laravel-4 guzzle