我想提取给定envato产品的价格 - http://codecanyon.net/item/ftp-cloud/7610011 直到现在我已经达到了这个目标: -
<?php
$url = "http://codecanyon.net/item/ftp-cloud/7610011";
$html = file_get_contents_curl($url);
function get_price($html)
{
$price = "";
$dom = new DOMDocument();
$dom->loadHTML($html);
$dom->preserveWhiteSpace = false;
$xpath = new DOMXPath($dom);
$divs_price = $xpath->query('//strong[@class="purchase-form__price js-purchase-price"]');
//print_r($divs_price);
foreach ($divs_price as $price_data)
{
$price = trim(innerHTML($price_data));
if($price!="")
return $price;
}
return $price;
}
$price = get_price($html);
echo $price;
但我的php输出中仍然没有得到任何响应。 补充:我用过这个
function file_get_contents_curl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.2 Safari/537.36");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
答案 0 :(得分:0)
file_get_contents_curl不是函数。你尝试过这样的事情吗?
$html = download($url);
function download($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec($ch);
curl_close($ch);
unset($ch);
return $data;
}
function get_price($html)
{
$pattern = '/<strong class="purchase-form__price js-purchase-price">(.*?)<\/strong>/s';
preg_match_all($pattern, $html, $result);
return trim($result[1][0]);
}
答案 1 :(得分:0)
不确定为什么你没有使用Envato API它很好地提取价格
http://marketplace.envato.com/api/documentation
这是代码,如果你喜欢
$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_URL, 'http://marketplace.envato.com/api/edge/item-prices:'.$itemid.'.json');
curl_setopt($ch1, CURLOPT_CONNECTTIMEOUT, 50);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
$ch_data1 = curl_exec($ch1);
$json_data1 = json_decode($ch_data1, true);
$price = $json_data1['item-prices']['0']['price'];
curl_close($ch1);
它甚至不需要你的envato api密钥。 itemid是codecanyon项的项ID。
代码来自http://codecanyon.net/item/sixthlife-search-for-envato-affiliates/7614796