Rade
Rade

Reputation: 35

Laravel returning UnexpectedValueException

I'm having some problems with scraping function in Laravel. The function is returning value of that scraped data, but below is showing a message like.

UnexpectedValueException: The Response content must be a string or object implementing __toString()

What am I doing wrong here?

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Goutte;

class ScraperController extends Controller
{

    public function index(){
        $price = $this->getPrice();
        return $price;
    }

    public function getPrice(){

        $final_price = '';
        $crawler = Goutte::request('GET', 'https://www.aliexpress.com/item/Cable-Chompers-Animal-Protectors-Bite-Cable-Bite-Protector-Saver-For-Iphone-USB-Charger-Cable-Cute-Cartoon/32917115384.html');

            $crawler->filter('#j-sku-price')->each(function ($node) {
            $price = $node->text();
            print($price);
            });

        return true;
    }
}

Upvotes: 1

Views: 169

Answers (2)

porloscerros Ψ
porloscerros Ψ

Reputation: 5088

The $price variable in the index function is taken a boolean value. In your getPrice() you must return a string (or object) instead of boolean. Eg:

public function getPrice(){
    // ...

    return $final_price; // if the value of $final_price is what you want
}

Upvotes: 1

Rade
Rade

Reputation: 35

Actually when I delete return true everything works fine. Thanks!

Upvotes: 0

Related Questions