我希望我的网站能够从此页面获取信息beta.tfgm.com/public-transport/tram/stops/oldham-central-tra m,特别是我想从该网站获取目的地和截止时间并给予我输出。
此时我的控制器看起来像这样:
function guzzle(){
$client = new \GuzzleHttp\Client();
$results = $client->get('https://beta.tfgm.com/public-transport/tram/stops/oldham-central-tram');
//$result->getBody();
//return view('hello')->with('results', $results);
//dd($result);
$results->getBody()->getContents();
dd($results);
return view('hello');
}
输出是:
Response {#190 ▼
-reasonPhrase: "OK"
-statusCode: 200
-headers: array:8 [▼
"Content-Security-Policy" => array:1 [▶]
"Content-Type" => array:1 [▶]
"Date" => array:1 [▶]
"ETag" => array:1 [▶]
"X-Content-Security-Policy" => array:1 [▶]
"X-WebKit-CSP" => array:1 [▶]
"Content-Length" => array:1 [▶]
"Connection" => array:1 [▶]
]
-headerNames: array:8 [▼
"content-security-policy" => "Content-Security-Policy"
"content-type" => "Content-Type"
"date" => "Date"
"etag" => "ETag"
"x-content-security-policy" => "X-Content-Security-Policy"
"x-webkit-csp" => "X-WebKit-CSP"
"content-length" => "Content-Length"
"connection" => "Connection"
]
-protocol: "1.1"
-stream: Stream {#188 ▶}
}
但是,如何访问特定的html类,从中抓取信息并将其输出到我的页面上?
答案 0 :(得分:1)
dd
对您没用,请尝试以下方法:
$client = new \GuzzleHttp\Client();
$results = $client->get('https://beta.tfgm.com/public-transport/tram/stops/oldham-central-tram');
print_r($results->getBody()->getContents()); // The HTML content