semantics3/semantics3-php

此包已被废弃,不再维护。未建议替代包。

Semantics3 API 的 PHP 库绑定

dev-master 2017-07-13 05:28 UTC

This package is not auto-updated.

Last update: 2023-04-01 06:31:42 UTC


README

semantics3-php 是访问 Semantics3 产品 API 的 PHP 绑定,该 API 为大量产品提供结构化信息,包括定价历史。更多信息请见 https://www.semantics3.com

快速入门指南: https://www.semantics3.com/quickstart API 文档可在 https://www.semantics3.com/docs/ 找到。

安装

可以通过 composer 安装 semantics3-php

php composer.phar install

要从仓库安装最新源代码

git clone https://github.com/Semantics3/semantics3-php.git

入门

为了使用客户端,您必须拥有 API 密钥和 API 密码。要获取密钥和密码,您需要先在 https://www.semantics3.com/ 创建账户。您可以从 https://dashboard.semantics3.com 的用户仪表板访问您的 API 访问凭证。

设置工作

让我们打下基础。

require('lib/Semantics3.php');

# Set up a client to talk to the Semantics3 API using your Semantics3 API Credentials
$key = 'SEM3xxxxxxxxxxxxxxxxxxxxxx';
$secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

$requestor = new Semantics3_Products($key,$secret);

第一次请求,也就是“Hello World”

让我们运行我们的第一次请求!我们将执行一个简单的搜索,搜索词为 "iPhone",如下所示

# Build the request
$requestor->products_field( "search", "iphone" );

# Run the request
$results = $requestor->get_products();

# View the results of the request
echo $results;

示例请求

以下请求显示了如何与 Semantics3 产品 API 的某些核心功能进行接口交互。更多详细示例请参阅快速入门指南: https://www.semantics3.com/quickstart

分页

“Hello World”脚本中的示例返回前 10 个结果。在这个例子中,我们将滚动到后续页面,超出初始请求

# Build the request
$requestor->products_field( "search", "iphone" );

# Run the request
$results = $requestor->get_products();

# View the results of the request
echo $results;

$page = 0
while ($results = $requestor->iterate_products()) {
    $page++;
    echo "We are at page = $page\n";
    echo "The results for this page are:\n";
    echo $results;
}

UPC 查询

运行 UPC/EAN/GTIN 查询与运行搜索查询一样简单

# Build the request
$requestor->products_field( "upc", "883974958450" );
$requestor->products_field( "field", ["name","gtins"] );

# Run the request
$results = $requestor->get_products();

# View the results of the request
echo $results;

URL 查询

明白了?您可以按以下方式运行 URL 查询

$requestor->products_field( "url", "http://www.walmart.com/ip/15833173" );
$results = $requestor->get_products();
echo $results;

价格过滤

使用 "lt"(小于)标签过滤价格

$requestor->products_field( "search", "iphone" );
$requestor->products_field( "price", "lt", 300 );
$results = $requestor->get_products();
echo $results;

类别 ID 查询

要查找关于 cat_id 的详细信息,请针对类别资源运行您的请求

# Build the request
$requestor->categories_field("cat_id", "4992");

# Run the request
$results = $requestor->get_products();

# View the results of the request
echo $results;

Webhooks

您可以使用 Webhooks 从 Semantics3 获取近乎实时的价格更新。

创建webhook

您可以通过向“webhooks”端点发送POST请求来在Semantics3上注册webhook。为了验证您的URL是否活跃,将向您的服务器发送带有verification_code参数的GET请求。您的服务器应通过响应体中的verification_code来完成验证过程。

$params["webhook_uri"] = "http://mydomain.com/webhooks-callback-url";


$results = $requestor->run_query("webhooks", $params, "POST");
echo $results;

获取现有webhook

$results = $requestor->run_query("webhooks", null);
echo $results;

删除webhook

$webhook_id = "7JcGN81u";

$endpoint = "webhooks/" . $webhook_id;

$results = $requestor->run_query($endpoint, null, "DELETE");
echo $results;

注册事件

一旦注册了webhook,您就可以开始向其中添加事件。当这些事件发生时,Semantics3服务器会向您发送通知。要为特定webhook注册事件,请向"webhooks/{webhook_id}/events"端点发送POST请求

$params = array(
    "type" => "price.change",
    "product" => array(
        "sem3_id" => "1QZC8wchX62eCYS2CACmka"
    ),
    "constraints" => array(
        "gte" => 10,
        "lte" => 100
    )
);

$webhook_id = "7JcGN81u";
$endpoint = "webhooks/" . $webhook_id . "/events";

$results = $requestor->run_query($endpoint, $params, "POST");
echo $results;

获取给定webhook的所有已注册事件

$webhook_id = "7JcGN81u";
$endpoint = "webhooks/" . $webhook_id . "/events";

$results = $requestor->run_query($endpoint, $params, "GET");
echo $results;

Webhook通知

一旦您创建了webhook并在其上注册了事件,当相应事件发生时,将通过POST请求将通知发送到您注册的webhook URI。请确保您的服务器可以接收POST请求。以下是一个示例通知对象的样子

{
    "type": "price.change",
    "event_id": "XyZgOZ5q",
    "notification_id": "X4jsdDsW",
    "changes": [{
        "site": "abc.com",
        "url": "http://www.abc.com/def",
        "previous_price": 45.50,
        "current_price": 41.00
    }, {
        "site": "walmart.com",
        "url": "http://www.walmart.com/ip/20671263",
        "previous_price": 34.00,
        "current_price": 42.00
    }]
}

故障排除

如果您收到“异常CURL错误:SSL证书问题,请验证CA证书是否正确”或类似错误,这表明远程SSL证书存在问题。

要修复此问题,您只需将数组(CURLOPT_SSL_VERIFYPEER => false)作为$curl_options参数传递给OAuthRequester::requestRequestToken即可。

或者,如果您正在使用ubuntu,您可以这样做

sudo apt-get install --reinstall ca-certificates

贡献

使用GitHub的标准fork/commit/pull-request周期。如果您有任何问题,请通过support@semantics3.com发送电子邮件。

作者

版权

版权(c)2015 Semantics3 Inc.

许可

The "MIT" License

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.