promoqui/promoqui-api-php

PHP版本的PromoQui API包装器

0.9.7 2017-04-06 10:51 UTC

This package is not auto-updated.

Last update: 2024-09-14 19:03:56 UTC


README

# PHP的PromoQui API包装器

此库仅限内部使用。它作为Ruby开发者与PromoQui REST API进行交互时的助手。

Settings类允许您设置API域的URL。

在没有PromoQui SPA许可的情况下,不要尝试使用此库对官方PromoQui API域进行操作。不过,您可以使用它来为自己的项目服务,并报告您可能遇到的问题。

设置

我们使用composer来管理我们的PQSDK包装器,因此您必须根据您的操作系统安装composer。

安装composer后,要安装PQSDK包装器,请创建一个新的文件夹,并在其中从终端运行

composer require 'promoqui/promoqui-api-php'

首先,您必须声明Crawlers命名空间,并使用PHP的require函数导入composer的autoload.php加载器

namespace Crawlers;

require 'autoload.php';

配置库以使用您提供的API主机、密钥、架构和国家/地区

Settings::$country = 'it';
Settings::$host = 'api.promotest.dev';
Settings::$app_secret = 'sup3rs3cr3t';
Settings::$schema = 'http'; // or https

PromoQui REST API需要将应用密钥与一个持续3小时的令牌进行交换。所有令牌交换和更新都由库内部处理,因此您可以完全忽略它。

与品牌协同工作

$brand = Brand::find('Apple');

该行代码将查询PromoQui数据库以获取该品牌,并返回一个包含id、name、slug等详情的Brand对象。

$brands = Brand::list();

该行代码将查询PromoQui数据库以获取所有品牌,并返回包含Brand对象的数组。

与城市协同工作

$city = City::find('Rome');

该行代码将查询Promoqui数据库以获取指定城市。如果该城市存在于我们的数据库中,它将返回一个包含该城市所有详情(如:name、latitude、longitude、inhabitants以及最重要的City ID)的City对象。

$city = City::find_or_create('Rome');

该行代码将查询PromoQui数据库以获取指定城市,如果未找到,则创建该城市,并返回一个包含所有详情(如:latitude、longitude、inhabitants以及最重要的City ID)的City对象。

要获取特定国家的城市,您必须使用类似以下内容的方法

$cities = array_filter(City->all(), function($val){return $val['country'] == 'gbr';}); # will return an array of City objects that havve only country=gbr

与商店协同工作

$store = Store::find('Via Roma, 32', '80100');
if ($store == null){
  $store = new Store();
  $store->$name = "Store name"; # Required!
  $store->$address = "Via Roma, 32"; # Required!
  $store->$city = "Naples"; # if the city is not present on database then the city will be created. Required!
  $store->$latitude = ""; # insert the store's latitude.
  $store->$longitude = "";# insert the store's longitude.
  $store->$zipcode = ""; # insert the store's postalcode. if there is no postalcode, insert "00000". Required!
  $store->$origin = ""; # insert the store's url. Required!
  $store->$phone = "";# insert the store's phone if present
}
$store->$opening_hours = [store_hours]; # Insert the store's opening hours as array. Required!
$store->save(); # Save store's data

##注意!## 开放时间数组必须如下所示

[["weekday"=>0, "open_am"=>"09:00", "close_am"=>"13:00", "open_pm"=>"14:00", "close_pm"=>"18:00"], ...]
If the store is closed you need to use such as: [ ["weekday"=>6, "closed"=>true] ]
The opening_hours must be in total 7 (one for every day) and must be uniq so please be carreful with this

此代码将查询数据库以获取位于该地址、该邮编的商店,以及分配给我们的零售商的商店。如果找不到商店,则将设置商店的所有数据并保存。

##关于Geocoder的使用## 如果您找不到商店的纬度/经度坐标,则必须跳过设置PQSDK::Store对象的纬度/经度。我们的服务器将仅在第一次使用geocoder来设置这些值。

与传单协同工作

$leaflet = Leaflet::find($url);
if ($leaflet == null) {
  $leaflet = new Leaflet();
  $leaflet->$name = 'Nice leaflet';
  $leaflet->$url = $url;
  $leaflet->$store_ids = [ $storeIds];
  $leaflet->save();
}

此代码将尝试找到具有相同URL的传单(以避免再次插入)。如果未找到,则将创建一个新的传单。请注意store_ids字段。它必须是一个包含有效商店ID的数组,对于这些商店,传单是有效的。

创建后几秒钟,PromoQui基础设施将开始解析和上传传单页面到网站。

如果您没有有效的GET URL从其中获取传单,但您能够获得传单的二进制版本(原始PDF数据字节),您仍然可以像这样上传它

$leaflet = new Leaflet();
$leaflet->$url = $url; # Set to a significant URL to avoid repetitions
$leaflet->$name = "Nice leaflet";
$leaflet->$store_ids = [ $storeIds ];
$leaflet->$pdf_data = $binary_blob;
$leaflet->save();

如果您有传单页面而不是传单的PDF URL或原始数据,您可以发送图像URL数组,如以下所示

$leaflet = new Leaflet();
$leaflet->$name = "leaflet's name";
$leaflet->$url = "leaflet's url";
$leaflet->$image_urls = [ leaflet_pages ]; # it must be an array of urls
$leaflet->$store_ids = [ storeIds ];
$leaflet->save();

#与优惠协同工作

对于每个需要解析的优惠

  • 报价的标题
  • 报价的描述
  • 报价的URL
  • 报价的图片URL
  • 报价的价格
  • 如有,报价的原价 如有

假设我们有一个名为 offers 的数组,其中包含所有报价,以及一个名为 storeIds 的数组,其中包含所有商店ID

foreach($offers as $data){
  $data["store_ids"] = $storeIds; #add store ids to offer array
  $offer = new Offer($data);
  $offer->save();
}

以上代码将遍历所有报价,将 storeIds 分配给报价并保存。

支持库

为了方便,我们在 "libraries" 文件夹中包含了 PHP 库 "simplehtmldom"(《http://simplehtmldom.sourceforge.net/》)用于解析HTML页面。

这是最常用的外部库,用于解析HTML页面。它用PHP5 +编写,允许你以非常简单的方式操作HTML。

文档可以在以下URL中找到: http://simplehtmldom.sourceforge.net/manual.htm

您必须在您的爬虫PHP脚本中包含以下包含代码

include_once "libraries/simple_html_dom.php";

示例

// Create DOM from URL or file
$html = file_get_html('http://www.google.com/');

// Find all images
foreach($html->find('img') as $element)
       echo $element->src . '<br>';

// Find all links
foreach($html->find('a') as $element)
       echo $element->href . '<br>';

祝您过得愉快