-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kinopoisk.php
79 lines (69 loc) · 1.88 KB
/
Kinopoisk.php
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
74
75
76
77
78
79
<?php
namespace bookin\parser;
class Kinopoisk
{
private function __construct()
{}
/**
* @param $url
* @return \stdClass
*/
public static function parseByUrl($url){
$command = implode(' ', [
'node',
__DIR__.DIRECTORY_SEPARATOR.'parser.js',
"'{$url}'",
]);
$exitCode = self::execute($command, null, $output, $output, 55);
return @json_decode($output, false)?:new \stdClass();
}
/**
* Run command
* @param $cmd
* @param null $stdin
* @param $stdout
* @param $stderr
* @param bool $timeout
* @return int
*/
protected static function execute($cmd, $stdin=null, &$stdout, &$stderr, $timeout=false){
$pipes = array();
$process = proc_open(
$cmd,
[['pipe','r'],['pipe','w'],['pipe','w']],
$pipes
);
$start = time();
$stdout = '';
$stderr = '';
if(is_resource($process))
{
stream_set_blocking($pipes[0], 0);
stream_set_blocking($pipes[1], 0);
stream_set_blocking($pipes[2], 0);
fwrite($pipes[0], $stdin);
fclose($pipes[0]);
}
while(is_resource($process))
{
//echo ".";
$stdout .= stream_get_contents($pipes[1]);
$stderr .= stream_get_contents($pipes[2]);
if($timeout !== false && time() - $start > $timeout)
{
proc_terminate($process, 9);
return 1;
}
$status = proc_get_status($process);
if(!$status['running'])
{
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($process);
return $status['exitcode'];
}
usleep(100000);
}
return 1;
}
}