簡體   English   中英

PHP json 新行和標簽

[英]PHP json new lines and tabs

我在顯示和格式化 JSON 代碼時遇到了一些問題。 我正在使用 Laravel goutte 抓取 Aliexpress 產品頁面,並且我已經提取了 aliexpress 在其源代碼中提供的 JSON(您可以在源代碼的末尾查看它以 Z05B8C74CBD96FBF2DE4C1A352702FFF4.runParam 開頭) 因此,在我成功提取該數據后,我在格式化 JSON 時遇到了問題。 如您所見,Aliexpress 已經在 JSON 中返回了該數據,因此我不需要編寫 json_encode 或解碼。 I'm returning my response in postman and I don't think that postman is making that JSON response wrong, when I open JSON formatter it's always giving me errors for multiple lines "Invalid character".

這是我的完整代碼,我只是調用這個 class 並在 controller 中返回它:

use Goutte\Client;
use Symfony\Component\HttpClient\HttpClient;

class Scrapers
{
    // This will be sent from another class, for now we are calling it from here
    private $client;
    public $url;
    public $array = [];

    public function __construct($url){
        $this->url = $url;
        $this->client = new Client(HttpClient::create(['timeout' => 60]));

        $this->client->request('GET', $url)->filter('script')->each(
            function ($node) {
                array_push($this->array, $node->html());
        });
    }

    public function getBetween($content,$start,$end){
        $r = explode($start, $content);
        if (isset($r[1])){
            $r = explode($end, $r[1]);
            return $r[0];
        }
        return '';
    }

    /**
     * Return product title
     */
    public function getTitle(){
        $content = $this->array[16];
        $start = "data:";
        $end = "csrfToken:";
        $output = $this->getBetween($content,$start,$end);

        $remove_new_lines = str_replace(array("\n", "\r"), '', $output);
        return $remove_new_lines;
    }

目前我沒有從文件末尾提取“,”,但如果我刪除它,格式化程序必須返回有效的 json 響應(但它沒有)。 這就是我的 postman 中返回的數據的方式: https://justpaste.it/3qrtm

我嘗試了多個刪除選項卡、新空間和我在互聯網上找到的所有內容的功能,但沒有成功。 任何想法如何解決這個問題?

嘗試$node->text()而不是$node->html()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM