首页 > 解决方案 > Laravel 返回 UnexpectedValueException

问题描述

我在 Laravel 中的抓取功能遇到了一些问题。该函数正在返回该抓取数据的值,但下面显示了类似的消息。

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

我在这里做错了什么?

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;
    }
}

标签: phplaravel

解决方案


$price索引函数中的变量采用布尔值。在你的getPrice()你必须返回一个string(或对象)而不是boolean. 例如:

public function getPrice(){
    // ...

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

推荐阅读