[英]How to get data from multiple URLs using curl in php?
我正在嘗試獲取多個產品的一些詳細信息,以下代碼適用於單個URL,並且工作正常:-
<?php
$url = "http://www.flipkart.com/healthgenie-hd-221-digital-black-dotted-weighing-scale/p/itmeatqzeehkdsmg";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($curl);
curl_close($curl);
$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($output);
libxml_clear_errors();
$xpath = new DOMXpath($dom);
// Product Name
$find_product_name = $xpath->query('//h1[@class="title"]');
if($find_product_name->length > 0)
{
$product_name= $find_product_name->item(0)->nodeValue;
}
else
{
$product_name="Product Name Not Found";
}
// Sold by
$find_seller = $xpath->query('//a[@class="seller-name"]');
if($find_seller->length > 0)
{
$seller= $find_seller->item(0)->nodeValue;
}
else
{
$seller="Seller Not Found";
}
// List Price
$find_list_price = $xpath->query('//span[@class="price"]');
if($find_list_price->length > 0)
{
$list_price= $find_list_price->item(0)->nodeValue;
}
else
{
$list_price="Not Available";
}
// Sale Price
$find_sale_price = $xpath->query('//span[@class="selling-price omniture-field"]');
if($find_sale_price->length > 0)
{
$sale_price= $find_sale_price->item(0)->nodeValue;
}
else
{
$sale_price="Not Available";
}
// Stock Status
$find_stock = $xpath->query('//div[@class="out-of-stock-status"]');
if($find_stock->length > 0)
{
$stock= $find_stock->item(0)->nodeValue;
}
else
{
$stock = "In Stock!";
}
//
?>
<table width="100%" align="center" border="1">
<tr>
<th>Product Name</th>
<th>Sold By</th>
<th>List Price</th>
<th>Sale Price</th>
<th>Stock Status</th>
</tr>
<tr>
<td><?php echo '<a href="'.$url.'" target="_blank">'.$product_name.'</a>'; ?></td>
<td><?php echo $seller; ?></td>
<td><?php echo $list_price; ?></td>
<td><?php echo $sale_price; ?></td>
<td><?php echo $stock; ?></td>
</tr>
</table>
現在,我需要一種從多個URL一次獲取數據的方法。 我希望對多個URL ny采取相同的過程ny以數組中的URL進行。例如:-
<?php
$url = array(
"http://www.flipkart.com/healthgenie-hd-221-digital-black-dotted-weighing-scale/p/itmeatqzeehkdsmg",
"http://www.flipkart.com/healthgenie-hd-221-digital-black-dotted-weighing-scale/p/itmeatqzeehkdsmg",
"http://www.flipkart.com/healthgenie-hd-221-digital-black-dotted-weighing-scale/p/itmeatqzeehkdsmg"
);
?>
我要求所有開發人員請看一下,並為此提供幫助。 提前謝謝了。
將網址放入CSV文件中,然后創建一個上傳表單。
<?php
$fsn = fopen($_FILES['fsn']['tmp_name'], 'r');
if(!$fsn)
{
die('Cannot open uploaded file.');
}
while (($data = fgetcsv($fsn)) !== FALSE)
{
$urls[] = $data[0];
}
foreach($urls as $url)
{
// Execute the code
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.