webshotapi/client

PHP 的 Webshotapi API 客户端

v1.0.12 2024-07-18 09:00 UTC

This package is auto-updated.

Last update: 2024-09-18 10:01:49 UTC


README

轻松捕获屏幕截图并保存为各种格式,如 JPG、PNG 和 PDF。此外,利用提取每个 HTML 元素的选择器、坐标和 CSS 样式的功能。使用我们的 API 无缝地启动项目并排队所有 URL。我们的服务器能够处理所有必要任务,简化您的流程。

安装

composer require webshotapi/client

在截图前移除Cookies弹出窗口

Remove cookies popup before take screenshot

利用创新的 AI 算法在捕获截图前无缝移除干扰的Cookies弹出窗口。 了解更多

文档

您可以在本网站上找到有关我们的 API 的完整文档 网站截图 API 文档

API密钥

注册后可以生成的 API 密钥。 https://dashboard.webshotapi.com/api_keys

使用

截图并保存为 jpg 文件

<?php

include_once __DIR__ . '/../vendor/autoload.php';

use Webshotapi\Client\WebshotApiClient;
use Webshotapi\Client\Exceptions\WebshotApiClientException;

try{

    $API_KEY = 'YOU_API_KEY';
    $URL = 'PUT_LINK_TO_WEBSITE_HERE';
    
    $SAVE_PATH = '/tmp/save2.jpg';
    
    $params = array(
        'remove_modals' => true, // Remove cookies popups before take screenshot
        'premium_proxy' => true, // use premium proxy 
        'geotargeting' => 'us', // use ip from USA
        'no_cache' => true, // Do not return result files from cache
        'ads' => '1', // Remove ads
        'width' => '1280',
        'height' => '2040',
        'image_quality' => 75,
        'transparent_background' => false,
        'full_page' => true,
    );
    
    $webshotapi = new WebshotApiClient($API_KEY);
    
    //Download, save jpg and send to browser
    $response = $webshotapi->screenshot($URL, $params);
    $response->save($SAVE_PATH);

} catch (WebshotApiClientException $e){
    echo"ERROR: ";
    echo $e->getMessage();
}

截图并保存为 PDF 文件

您可以将 HTML 页面转换为 PDF 发票。

<?php

include_once __DIR__ . '/../vendor/autoload.php';

use Webshotapi\Client\WebshotApiClient;
use Webshotapi\Client\Exceptions\WebshotApiClientException;

try{
    
    $API_KEY = 'YOU_API_KEY';
    $URL = 'PUT_LINK_TO_WEBSITE_HERE';
    
    $SAVE_PATH = '/tmp/save2.pdf';
    
    $params = array(
        'width' => '1280',
        'height' => '2040',
        'no_cache' => '1',
        'scroll_to_bottom' => '0',
        'wait_for_selector' => '',
        'wait_for_xpath' => '',
        'image_quality' => 75,
        'transparent_background' => false,
        'user_agent' => '',
        'accept_language' => '',
        'full_page' => true,
        'timezone' => '',
        'fail_statuscode' => '',
        'capture_element_selector' => '',
    );
    
    $webshotapi = new WebshotApiClient($API_KEY);
    
    //Download, save jpg and send to browser
    $response = $webshotapi->pdf($URL, $params);
    $response->save($SAVE_PATH);
    

} catch (WebshotApiClientException $e){
    echo"ERROR: ";
    echo $e->getMessage();
}

渲染后提取文本地图和具有 CSS 样式的 HTML 元素

独特的软件,从网站中提取 HTML 元素的所有选择器以及渲染后的 CSS 样式。此外,您可以提取所有带有位置的单词(x,y,width, height, 相对于前一个单词的偏移量)。因此,您可以构建网站的单词地图。

示例脚本

<?php

include_once __DIR__ . '/../vendor/autoload.php';

use Webshotapi\Client\WebshotApiClient;
use Webshotapi\Client\Exceptions\WebshotApiClientException;

try{

$API_KEY = 'YOU_API_KEY';
$URL = 'PUT_LINK_TO_WEBSITE_HERE';

$SAVE_PATH = '/tmp/save2.json';

$params = array(
    [
        'no_cache'=>1,
        'extract_selectors'=>1,
        'extract_words' => 1,
        'extract_style' => 1,//0 - skip styles, 1 - download most import css styles, 2 - download all styles for element
    ]
);

$webshotapi = new WebshotApiClient($API_KEY);

//Download, save jpg and send to browser
$response = $webshotapi->extract($URL, $params);

// Save to file
$response->save($SAVE_PATH);

// If you want to manipulate json f
$json_data = $response->json();



} catch (WebshotApiClientException $e){
echo"ERROR: ";
echo $e->getMessage();
}

提取方法的示例结果 json

{
  "status_code": 200,
  "html": "<!doctype html><html lang='en' dir='ltr'><head><base hr...",
  "text": "Welcome in our page\\nToday is Monday..",
  "screenshot_url": "https://api.webshotapi.com/v1/screenshot/?token=....&width=1920&height=960",
  "selectors": [
    {
      "class": ".col-12 col-lg-12",
      "id": "price",
      "itemprop": "title",
      "x": 434.8,
      "y": 343.4,
      "w": 434,
      "h": 43,
      "xpath": "/html/body/div[1]/div[2]/div/div[2]/ul/li[5]/ul/li[2]",
      "style": {
        "visibility": "visible",
        "display": "block",
        "fontWeight": "400",
        "backgroundImage": "none",
        "cursor": "pointer",
        "fontSize": "22px",
        "color": "rgb(255, 255, 255)",
        "position": "absolute",
        "textDecoration": "none solid rgb(255, 255, 255)",
        "textDecorationLine": "none",
        "backgroundColor": "rgba(0, 255, 255, 0)"
      }
    }
  ],
  "words": [
    {
      "xpath": "/html/body/div[1]/div[2]/div/div[2]/ul/li[5]/a",
      "word": "Hello",
      "word_index": 2,
      "offset": 14,
      "position": {
        "x": 434.8,
        "y": 343.4,
        "w": 434,
        "h": 43
      }
    }
  ]
}

API 文档

您可以在本网站上找到有关我们的 API 的完整文档 http://webshotapi.com/docs/ 对于 PHP 客户端文档:API 文档

许可证

MIT