【问题标题】:Extract Price from Given Envato Marketplace [closed]从 Given Envato 市场中提取价格 [关闭]
【发布时间】:2014-04-29 05:58:20
【问题描述】:

我想提取给定 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;
}

【问题讨论】:

  • 然后呢?什么不工作
  • 不是file_get_contents()还是用户自定义函数?
  • 这是用户定义的函数。我现在已经添加了有问题的详细信息!

标签: php file-get-contents


【解决方案1】:

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

【讨论】:

  • 我已将其添加到问题中
  • 我修改了你的 getPrice 方法,试试这个:function get_price($html) { $pattern = '/(.*?)/s'; preg_match_all($pattern, $html, $result);返回修剪($结果[1][0]); }
  • 谢谢! lulco 在您的答案中添加此代码...使其成为接受的答案!
【解决方案2】:

不知道您为什么不使用 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 item 的 item id。

代码取自http://codecanyon.net/item/sixthlife-search-for-envato-affiliates/7614796

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-06
    相关资源
    最近更新 更多