效果

PHP 客户端库简化了与 Google Ads API 的互动,所需 配置。不过,效果在很大程度上取决于 使用和集成了库。

这些最佳做法中的大部分都适用, 支持所有语言。本指南介绍了 PHP 特有的用法。

Protobuf 实现

ProtobufgRPC 使用 以及用于请求和响应消息的 Google Ads API。两种实现方法 只不过以 C 语言编写的代码效果更好。

如需了解详情,请参阅 Protobuf 指南

PHP 解释器的操作模式

PHP 是一种用途多样的脚本 language,并且有许多操作 模式。菲律宾比索 CGI(通用网关接口)的优势在于它可以与 资源。

PHP 版本

最好像往常一样定期升级到较新的 PHP 版本 从而实现更好的整体效果支持的 PHP 的列表 版本

未使用的 Google Ads API 版本

所有版本的客户端库都支持多个 Google Ads API 版本。 对于客户端库支持的各个 Google Ads API 版本,有 专用软件包

未使用的 Google Ads API 版本专用软件包 已从客户端库中安全移除因为它有助于加快 或减少内存占用,那么客户端库会提供相应的实用程序, 以编程方式创建

示例

假设您要实现的客户端库仅使用最新的 API 版本:v17,并且您想取消对未使用的 API 版本的支持 API 版本:v16v15

在项目的 composer.json 文件中,定义一个 Composer 脚本(名为 remove-google-ads-api-version-support),该函数利用 客户端库中的类 ApiVersionSupport:

"scripts": {
  "remove-google-ads-api-version-support": [
    "Google\\Ads\\GoogleAds\\Util\\ApiVersionSupport::remove"
  ]
}

然后,使用 Composer 脚本并将版本号作为参数,并输出 一些状态消息:

# Change the current directory to the project directory.
cd /path/to/the/project

# Install the project.
composer install

# Output the vendor folder size and the list of Google Ads API versions that are
# supported before removing support for Google Ads API versions.
echo "# Supported Google Ads API versions:"
find ./vendor/googleads/google-ads-php/src/Google/Ads/GoogleAds/V* -maxdepth 0 | grep -o '..$'
echo "# Vendor folder size:"
du -sh ./vendor

# Use the Composer script to remove the unused versions v15 and v16 of the Google Ads API.
echo "# Removing support..."
composer run-script remove-google-ads-api-version-support -- 15 16

# Output the vendor folder size and the list of Google Ads API versions that are
# supported after removing support for Google Ads API versions.
echo "# Supported Google Ads API versions:"
find ./vendor/googleads/google-ads-php/src/Google/Ads/GoogleAds/V* -maxdepth 0 | grep -o '..$'
echo "# Vendor folder size:"
du -sh ./vendor

下面的示例执行输出表明文件缩小了 50M,并且 唯一受支持的版本是 V17

# Supported Google Ads API versions:
V15
V16
V17
# Vendor folder size:
110M    ./vendor
# Removing support...
> Google\Ads\GoogleAds\Util\ApiVersionSupport::remove
Removing support for the version 15 of Google Ads API...
Done
Removing support for the version 16 of Google Ads API...
Done
# Supported Google Ads API versions:
V17
# Vendor folder size:
60M     ./vendor

开发与生产

PHP 是一种解释语言 因为它会先编译指令,然后再执行指令。这通常是 很有优势,因为在开发阶段,来源经常在执行时发生变化 时间并不那么重要然而,生产时情况正好相反, 稳定性和性能成了主要的考虑因素

缓存

缓存是一种很常见的做法,强烈建议这样做,因为它可以提高性能 并通过存储预编译的脚本指令来提高稳定性。

OPcache 是 解决方案,并且默认提供。

自动充值

自动加载很常见 因为这样既可以提升性能,又能提高稳定性 与类有关的预编译信息

PHP 客户端库符合 PSR-4 并作为 composer.json 文件。Composer 的专属选项,例如 然后可将 --optimize-autoloader--classmap-authoritative 转换为 开箱即可使用。

日志记录

将日志记录器的级别设置得较高(例如 ERROR)有助于缩短执行时间 开销和内存消耗

如需了解详情,请参阅 Logging 指南

调试和分析

我们建议停用调试程序和性能分析器工具,因为它们通常自带 有一些执行时间开销。

预加载

从 PHP 7.4 开始,OPcache 预加载 可用于预加载内存中的脚本,比常规 缓存。

脚本必须设计成使用该功能,但 PHP 代码 客户端库则不支持,因为不存在实现 OPcache 的通用方法 内存使用和性能增益之间的权衡是 与给定项目和执行密切相关。