业务:访问一个url,触发一个方法,这个方法中自己拼接了一个http请求,利用php的fsockopen函数、以及fwrite函数,再次发起一个get或者post请求,实现php的多线程(这个应该是多线程吧)
问题:用这样的方式,拼接get请求可以成功访问,但是拼接post请求一直不能成功(我再次触发的方法中是忘一个文件中写入东西,get可以成功写入,post不行).
访问的代码如下
public function ForFsockopen () {
$host = '127.0.0.1:8000';
$port = 80;
$errno = '';
$errstr = '';
$timeout = 30;
$url = '/testfsock';
$param = array(
'name' => 'fdipzone',
'gender' => 'man'
);
$data = http_build_query($param);
// create connect
$fp = fsockopen($host, $port, $errno, $errstr, $timeout);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
// $out = "GET $url HTTP/1.1\r\n";
// $out .= "Host: $host\r\n";
// $out .= "Connection:close\r\n\r\n";
$out = "POST $url HTTP/1.1\r\n";
$out .= "Host: $host\r\n";
$out .= "Content-Type: application/x-www-form-urlencoded\r\n";
$out .= "Content-Length: ".strlen($data)."\r\n";
$out .= "Referer: http://".$host.$url."\r\n";
$out .= "X-CSRF-TOKEN:csrf_token()\r\n";
$out .= "Cookie: username=admin;uid=200\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
fclose($fp);
}
}
再次发起请求触发的代码如下
public function TestFsock () {
$fp = fopen('file/test.txt', 'a');
for ($i = 0; $i < 1000; $i ++ ) {
fwrite($fp, '888888');
}
fclose($fp);
}
路由如下
Route::get('/forsockopen', "TestController@ForFsockopen");
Route::post('/testfsock', "TestController@TestFsock");
// Route::get('/testfsock', "TestController@TestFsock");