|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* ## Asynchronous |
|
4
|
|
|
* This function will open socket and ask socket to initiate php call by given $url and parameters, |
|
5
|
|
|
* In that function we have functionality of our needs. It just initiate just url with parameters |
|
6
|
|
|
* and returns your process will complete soon so, it's work like asynchronous. |
|
7
|
|
|
* |
|
8
|
|
|
* User: Gaurang Joshi |
|
9
|
|
|
* Date: 31/05/2017 |
|
10
|
|
|
* Time: 4:33 PM |
|
11
|
|
|
* |
|
12
|
|
|
* @throws Throwable |
|
13
|
|
|
* @author Gaurang Joshi <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace Asynchronous; |
|
17
|
|
|
|
|
18
|
|
|
use Throwable; |
|
19
|
|
|
use Exception; |
|
20
|
|
|
use Illuminate\Support\Facades\Log; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* ### Asynchronous |
|
24
|
|
|
* Class Asynchronous |
|
25
|
|
|
* @package Asynchronous |
|
26
|
|
|
*/ |
|
27
|
|
|
class Asynchronous { |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* ## Thread |
|
31
|
|
|
* Function to start php server url as in saperate thread |
|
32
|
|
|
* |
|
33
|
|
|
* @param string $url |
|
34
|
|
|
* @param string $params |
|
35
|
|
|
* |
|
36
|
|
|
* @return void |
|
37
|
|
|
* @throws Throwable |
|
38
|
|
|
*/ |
|
39
|
|
|
public static function thread($url, $params) { |
|
40
|
|
|
try { |
|
41
|
|
|
Self::write_log('info', 'ENTRY : thread :: Asynchronous.php'); |
|
42
|
|
|
Self::write_log('debug', 'DEBUG : URL = ' . $url . ' PARAM = ' . json_encode($params)); |
|
43
|
|
|
$post_string = http_build_query($params); |
|
|
|
|
|
|
44
|
|
|
$parts = parse_url($url); |
|
45
|
|
|
$err_no = 0; |
|
46
|
|
|
$err_str = ""; |
|
47
|
|
|
/* |
|
48
|
|
|
* Checking is given $url for opening as thread is secure server or non-secure server |
|
49
|
|
|
*/ |
|
50
|
|
|
if (parse_url($url, PHP_URL_SCHEME) === 'https') { |
|
51
|
|
|
// Use SSL & port 443 for secure servers (For secure server) |
|
52
|
|
|
$fp = fsockopen('ssl://' . $parts['host'], isset($parts['port']) ? $parts['port'] : 443, $err_no, $err_str, 30); |
|
53
|
|
|
} else { |
|
54
|
|
|
// Use otherwise for localhost and non-secure servers (For non-secure server) |
|
55
|
|
|
$fp = fsockopen($parts['host'], isset($parts['port']) ? $parts['port'] : 80, $err_no, $err_str, 30); |
|
56
|
|
|
} |
|
57
|
|
|
if (!$fp) { |
|
|
|
|
|
|
58
|
|
|
Self::write_log('critical', 'Woh! An Error occurs while creating socket connection.'); |
|
59
|
|
|
} |
|
60
|
|
|
$out = "POST " . $parts['path'] . " HTTP/1.1\r\n"; |
|
61
|
|
|
$out .= "Host: " . $parts['host'] . "\r\n"; |
|
62
|
|
|
$out .= "Content-Type: application/x-www-form-urlencoded\r\n"; |
|
63
|
|
|
$out .= "Content-Length: " . strlen($post_string) . "\r\n"; |
|
64
|
|
|
$out .= "Connection: Close\r\n\r\n"; |
|
65
|
|
|
if (isset($post_string)) { |
|
66
|
|
|
$out .= $post_string; |
|
67
|
|
|
} |
|
68
|
|
|
fwrite(/** @scrutinizer ignore-type */$fp, $out); |
|
69
|
|
|
fclose(/** @scrutinizer ignore-type */$fp); |
|
70
|
|
|
} catch (Exception $exception) { |
|
71
|
|
|
Self::write_log( |
|
72
|
|
|
'critical', |
|
73
|
|
|
'Exception occurs while calling Asynchronous thread function, |
|
74
|
|
|
Exception Message : '.$exception->getMessage() |
|
75
|
|
|
); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Creating function for print the longs to the logs file, created by application on configuration basis |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $level |
|
83
|
|
|
* @param string $message |
|
84
|
|
|
* |
|
85
|
|
|
* @return void |
|
86
|
|
|
* @throws Throwable |
|
87
|
|
|
* |
|
88
|
|
|
*/ |
|
89
|
|
|
private static function write_log($level, $message) { |
|
90
|
|
|
try { |
|
91
|
|
|
Log::stack(array('stack'))->log($level, $message); |
|
92
|
|
|
} catch (Throwable $throwable) { |
|
93
|
|
|
throw $throwable; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|