Php 从Flipkart API获取数据
我想使用Flipkart的API(严格使用PHP)从Flipkart获取产品详细信息。我想创建一个搜索栏,用户可以在其中输入任何“查询”,只需显示Flipkart的相关搜索结果。我不知道Php 从Flipkart API获取数据,php,web-scraping,Php,Web Scraping,我想使用Flipkart的API(严格使用PHP)从Flipkart获取产品详细信息。我想创建一个搜索栏,用户可以在其中输入任何“查询”,只需显示Flipkart的相关搜索结果。我不知道 <form action="test.php" method="get"> <input type="text" name="searchdata"> <label for="searchdata">Search Product<
<form action="test.php" method="get">
<input type="text" name="searchdata">
<label for="searchdata">Search Product</label>
<input id="search" type="submit" value="Search">
</form>
<?php
// Get Web Page Data using URL
echo "Flipkart";
if(isset($_GET['searchdata'])){
$search= $_GET['searchdata'];
$search= strtolower($search);
//space to plus
$search=str_replace(" ","+",$search);
$web_page_data = file_get_contents("https://flipkart.com/search?q=".$search);
echo "Data Output of above link"."<br/>";
echo "--------------------------------"."<br/>";
// We need particular data from the page and not the whole content
$item_list =explode('<div class="items-wrap">', $web_page_data); //from entire web page it will split based on word <div class="items-wrap">
//Item list is an array so print_r
//print_r($item_list);
$request = "https://affiliate-api.flipkart.net/affiliate/api/yuganshch.json";
$response = file_get_contents($request);
$results = json_decode($response, TRUE);
foreach ($results['data'] as $key => $itemdata) {
$price = $itemdata['Best_Price'];
$product_link = $itemdata['Uri'];
echo $seller."<br/>".$price."<br/>".$product_link."<br/><br/>";
}
}
else {
echo "<p>No Product Found, Please Search Again</p>";
}
搜索产品
快速测试在解析json响应后似乎出现了问题。json中似乎没有“data”元素($results['data']
),因此foreach
无法迭代未定义的索引。我只在该端点处看到“title”、“description”和“apiGroups”,而不是“data”,告诉我们错误并向我们显示$results
。如果您不打算在循环中使用$key
,请不要在循环开始时声明它。那么$results['data']
应替换为哪个变量@JASONWP可能只需$results就可以获得最高级别的JSON响应。快速测试在解析JSON响应后似乎有问题。json中似乎没有“data”元素($results['data']
),因此foreach
无法迭代未定义的索引。我只在该端点处看到“title”、“description”和“apiGroups”,而不是“data”,告诉我们错误并向我们显示$results
。如果您不打算在循环中使用$key
,请不要在循环开始时声明它。那么$results['data']
应替换为哪个变量@JASONWP可能只需$results即可获得最高级别的JSON响应。