-
Notifications
You must be signed in to change notification settings - Fork 292
Expand file tree
/
Copy path_crawler_bridge.php
More file actions
73 lines (57 loc) · 2.22 KB
/
_crawler_bridge.php
File metadata and controls
73 lines (57 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
// crawler_bridge.php
// Bridge script for Python to call PHP Spider methods and get JSON output.
// Usage: php crawler_bridge.php <spider_path> <method> [args...]
ini_set('display_errors', 0); // Disable error printing to stdout
error_reporting(E_ALL);
date_default_timezone_set('Asia/Shanghai');
header('Content-Type: application/json');
$output = ['status' => 'error', 'data' => null, 'message' => ''];
try {
if ($argc < 3) {
throw new Exception("Usage: php crawler_bridge.php <spider_path> <method> [args...]");
}
$spiderPath = $argv[1];
$method = $argv[2];
$args = array_slice($argv, 3);
if (!file_exists($spiderPath)) {
throw new Exception("Spider file not found: $spiderPath");
}
// Capture any output during include
ob_start();
require_once $spiderPath;
ob_end_clean();
if (!class_exists('Spider')) {
throw new Exception("Class 'Spider' not found in $spiderPath");
}
$spider = new Spider();
if (method_exists($spider, 'init')) {
$spider->init();
}
if (!method_exists($spider, $method)) {
throw new Exception("Method '$method' not found in Spider class");
}
// Call method with args
// Note: Args passed from CLI are strings. Some methods might expect specific types.
// However, PHP is loosely typed, so it usually works.
// Special handling for extend field or complex structures might be needed if passed via CLI,
// but standard DrPy methods usually take simple scalars (tid, page, filter) or arrays.
// For complex args (like filter array), we might need to decode JSON passed as string.
$methodArgs = [];
foreach ($args as $arg) {
// Try to decode JSON args if they look like JSON
$decoded = json_decode($arg, true);
if (json_last_error() === JSON_ERROR_NONE) {
$methodArgs[] = $decoded;
} else {
$methodArgs[] = $arg;
}
}
$result = call_user_func_array([$spider, $method], $methodArgs);
$output['status'] = 'success';
$output['data'] = $result;
} catch (Exception $e) {
$output['message'] = $e->getMessage();
$output['trace'] = $e->getTraceAsString();
}
echo json_encode($output, JSON_UNESCAPED_UNICODE);