-1

I have some problems with showing and formatting my JSON code. I'm scraping Aliexpress product page, with Laravel goutte, and I have extracted JSON that is provided by aliexpress in their source code (You can check it it starts with window.runParams at the end of source code). So after I successfully extracted that data, I have problems with formatting JSON. As you see Aliexpress already returned that data in JSON so I don't need to write json_encode or decode. 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".

This is my full code, and I'm just calling this class and returning it in 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;
    }

For now I don't have extracted ',' from the end of the file, but if I delete it, formatter must return valid json response anyway (but it doesen't). This is how is data returned in my postman: https://justpaste.it/3qrtm

I tried multiple functions that are removing tabs, new spaces and everything I found on internet, but no success. Any ideas how to fix that?

Rade Ilijev
  • 51
  • 1
  • 10

1 Answers1

-1

Try $node->text()instead of $node->html()

ybenhssaien
  • 3,427
  • 1
  • 10
  • 12