我需要从 https://www.fiverr.com/中抓取数据
基本上我实际上需要从以下子页面读取数据https://www.fiverr.com/gigs/gigs_as_json?host=search&type=single_query&query_string=pet&search_filter=rating&category_id=3&sub_category_id=49&limit=48
$url = 'https://www.fiverr.com/gigs/gigs_as_json?host=search&type=single_query&query_string=pet&search_filter=rating&category_id=3&sub_category_id=49&limit=48';
$html = file_get_contents( $url);
但它不是重新数据。我也尝试过卷发。根本没有成功。
有什么想法吗?
您尝试获取的网站会尝试设置一些 Cookie,然后告诉您使用新 Cookie 向同一 URL 发送另一个请求。PHP 的 file_get_contents()
默认情况下不发送 cookie,但它遵循重定向,这意味着您进入 302 重定向循环。
要避免此循环,您需要手动设置 cookie :
$url = 'http://www.fiverr.com/gigs/gigs_as_json?host=search&type=single_query&query_string=pet&search_filter=rating&category_id=3&sub_category_id=49&limit=48';
$opts = array('http' => array(
'header' => 'Cookie: locale=en%3B0%3Bfalse; suggested_locale=1;',
));
$ctx = stream_context_create($opts);
$data = file_get_contents($url, false, $ctx);
变量$data
包含二进制数据,因为网站压缩了内容。所以你可能想要有纯数据:
$data = gzdecode($data);
现在,您已经有了 JSON 编码的数据,可以使用 json_decode()
进行解析。