PHP で非同期 HTTP 呼び出しを行う方法はありますか? のようなことをしたいだけですがfile_get_contents()、残りのコードを実行する前にリクエストが完了するのを待ちません。これは、アプリケーションである種の「イベント」を開始したり、長いプロセスをトリガーしたりするのに非常に役立ちます。
何か案は?
PHP で非同期 HTTP 呼び出しを行う方法はありますか? のようなことをしたいだけですがfile_get_contents()、残りのコードを実行する前にリクエストが完了するのを待ちません。これは、アプリケーションである種の「イベント」を開始したり、長いプロセスをトリガーしたりするのに非常に役立ちます。
何か案は?
以前に受け入れた答えはうまくいきませんでした。それはまだ応答を待っていました。PHP で非同期 GET リクエストを作成するにはどうすればよいですか?
function post_without_wait($url, $params)
{
    foreach ($params as $key => &$val) {
      if (is_array($val)) $val = implode(',', $val);
        $post_params[] = $key.'='.urlencode($val);
    }
    $post_string = implode('&', $post_params);
    $parts=parse_url($url);
    $fp = fsockopen($parts['host'],
        isset($parts['port'])?$parts['port']:80,
        $errno, $errstr, 30);
    $out = "POST ".$parts['path']." HTTP/1.1\r\n";
    $out.= "Host: ".$parts['host']."\r\n";
    $out.= "Content-Type: application/x-www-form-urlencoded\r\n";
    $out.= "Content-Length: ".strlen($post_string)."\r\n";
    $out.= "Connection: Close\r\n\r\n";
    if (isset($post_string)) $out.= $post_string;
    fwrite($fp, $out);
    fclose($fp);
}
非同期で呼び出したいターゲットを制御している場合 (たとえば、独自の「longtask.php」)、その端から接続を閉じることができ、両方のスクリプトが並行して実行されます。それはこのように動作します:
私はこれを試しましたが、うまくいきます。しかし、プロセス間の通信手段を作成しない限り、quick.php は longtask.php がどのように動作しているかについて何も知りません。
他のことをする前に、このコードを longtask.php で試してください。接続は閉じますが、引き続き実行されます (出力は抑制されます)。
while(ob_get_level()) ob_end_clean();
header('Connection: close');
ignore_user_abort();
ob_start();
echo('Connection Closed');
$size = ob_get_length();
header("Content-Length: $size");
ob_end_flush();
flush();
コードは、PHP マニュアルのユーザー投稿ノートからコピーされ、多少改善されています。
exec()を使用して、のようなHTTPリクエストを実行できるものを呼び出すことでトリックを行うことができますがwget、プログラムからのすべての出力をファイルや/ dev / nullなどの場所に送信する必要があります。そうしないと、PHPプロセスはその出力を待機します。 。
プロセスをapacheスレッドから完全に分離したい場合は、次のようなことを試してください(これについてはよくわかりませんが、アイデアが得られることを願っています)。
exec('bash -c "wget -O (url goes here) > /dev/null 2>&1 &"');
これは良いビジネスではありません。実際のデータベースイベントキューをポーリングして実際の非同期イベントを実行するハートビートスクリプトを呼び出すcronジョブのようなものが必要になる可能性があります。
このライブラリを使用できます: https://github.com/stil/curl-easy
それはとても簡単です:
<?php
$request = new cURL\Request('http://yahoo.com/');
$request->getOptions()->set(CURLOPT_RETURNTRANSFER, true);
// Specify function to be called when your request is complete
$request->addListener('complete', function (cURL\Event $event) {
    $response = $event->response;
    $httpCode = $response->getInfo(CURLINFO_HTTP_CODE);
    $html = $response->getContent();
    echo "\nDone.\n";
});
// Loop below will run as long as request is processed
$timeStart = microtime(true);
while ($request->socketPerform()) {
    printf("Running time: %dms    \r", (microtime(true) - $timeStart)*1000);
    // Here you can do anything else, while your request is in progress
}
以下に、上記の例のコンソール出力を示します。リクエストが実行されている時間を示す単純なライブ時計が表示されます。

/**
 * Asynchronously execute/include a PHP file. Does not record the output of the file anywhere. 
 *
 * @param string $filename              file to execute, relative to calling script
 * @param string $options               (optional) arguments to pass to file via the command line
 */ 
function asyncInclude($filename, $options = '') {
    exec("/path/to/php -f {$filename} {$options} >> /dev/null &");
}
CURL低い設定を使用してリクエストの中止を偽装するCURLOPT_TIMEOUT_MS 
ignore_user_abort(true)接続が閉じられた後も処理を続けるように設定します。
この方法を使用すると、OS、ブラウザー、および PHP のバージョンに依存しすぎるヘッダーとバッファーを介して接続処理を実装する必要がなくなります。
マスタープロセス
function async_curl($background_process=''){
    //-------------get curl contents----------------
    $ch = curl_init($background_process);
    curl_setopt_array($ch, array(
        CURLOPT_HEADER => 0,
        CURLOPT_RETURNTRANSFER =>true,
        CURLOPT_NOSIGNAL => 1, //to timeout immediately if the value is < 1000 ms
        CURLOPT_TIMEOUT_MS => 50, //The maximum number of mseconds to allow cURL functions to execute
        CURLOPT_VERBOSE => 1,
        CURLOPT_HEADER => 1
    ));
    $out = curl_exec($ch);
    //-------------parse curl contents----------------
    //$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
    //$header = substr($out, 0, $header_size);
    //$body = substr($out, $header_size);
    curl_close($ch);
    return true;
}
async_curl('http://example.com/background_process_1.php');
バックグラウンド プロセス
ignore_user_abort(true);
//do something...
注意
cURL を 1 秒未満でタイムアウトさせたい場合は、CURLOPT_TIMEOUT_MS を使用できますが、値が < 1000 ミリ秒でエラーが発生した場合に libcurl をすぐにタイムアウトさせるバグ/「機能」が「Unix ライクなシステム」にあります。 cURL エラー (28): タイムアウトに達しました". この動作の説明は次のとおりです。
[...]
解決策は、 CURLOPT_NOSIGNAL を使用してシグナルを無効にすることです
資力
スウールエクステンション。https://github.com/matyhtf/swoole PHP 用の非同期および同時ネットワーク フレームワーク。
$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
$client->on("connect", function($cli) {
    $cli->send("hello world\n");
});
$client->on("receive", function($cli, $data){
    echo "Receive: $data\n";
});
$client->on("error", function($cli){
    echo "connect fail\n";
});
$client->on("close", function($cli){
    echo "close\n";
});
$client->connect('127.0.0.1', 9501, 0.5);
私のやり方をお見せしましょう:)
サーバーにnodejsをインストールする必要があります
(私のサーバーは 1000 の https get リクエストを送信し、2 秒しかかかりません)
url.php :
<?
$urls = array_fill(0, 100, 'http://google.com/blank.html');
function execinbackground($cmd) { 
    if (substr(php_uname(), 0, 7) == "Windows"){ 
        pclose(popen("start /B ". $cmd, "r"));  
    } 
    else { 
        exec($cmd . " > /dev/null &");   
    } 
} 
fwite(fopen("urls.txt","w"),implode("\n",$urls);
execinbackground("nodejs urlscript.js urls.txt");
// { do your work while get requests being executed.. }
?>
urlscript.js >
var https = require('https');
var url = require('url');
var http = require('http');
var fs = require('fs');
var dosya = process.argv[2];
var logdosya = 'log.txt';
var count=0;
http.globalAgent.maxSockets = 300;
https.globalAgent.maxSockets = 300;
setTimeout(timeout,100000); // maximum execution time (in ms)
function trim(string) {
    return string.replace(/^\s*|\s*$/g, '')
}
fs.readFile(process.argv[2], 'utf8', function (err, data) {
    if (err) {
        throw err;
    }
    parcala(data);
});
function parcala(data) {
    var data = data.split("\n");
    count=''+data.length+'-'+data[1];
    data.forEach(function (d) {
        req(trim(d));
    });
    /*
    fs.unlink(dosya, function d() {
        console.log('<%s> file deleted', dosya);
    });
    */
}
function req(link) {
    var linkinfo = url.parse(link);
    if (linkinfo.protocol == 'https:') {
        var options = {
        host: linkinfo.host,
        port: 443,
        path: linkinfo.path,
        method: 'GET'
    };
https.get(options, function(res) {res.on('data', function(d) {});}).on('error', function(e) {console.error(e);});
    } else {
    var options = {
        host: linkinfo.host,
        port: 80,
        path: linkinfo.path,
        method: 'GET'
    };        
http.get(options, function(res) {res.on('data', function(d) {});}).on('error', function(e) {console.error(e);});
    }
}
process.on('exit', onExit);
function onExit() {
    log();
}
function timeout()
{
console.log("i am too far gone");process.exit();
}
function log() 
{
    var fd = fs.openSync(logdosya, 'a+');
    fs.writeSync(fd, dosya + '-'+count+'\n');
    fs.closeSync(fd);
}
class async_file_get_contents extends Thread{
    public $ret;
    public $url;
    public $finished;
        public function __construct($url) {
        $this->finished=false;
        $this->url=$url;
    }
        public function run() {
        $this->ret=file_get_contents($this->url);
        $this->finished=true;
    }
}
$afgc=new async_file_get_contents("http://example.org/file.ext");
これは、任意のページの特定の URL に POST を実行するときの独自の PHP 関数です... サンプル: *** 関数の使用法...
    <?php
        parse_str("email=myemail@ehehehahaha.com&subject=this is just a test");
        $_POST['email']=$email;
        $_POST['subject']=$subject;
        echo HTTP_POST("http://example.com/mail.php",$_POST);***
    exit;
    ?>
    <?php
    /*********HTTP POST using FSOCKOPEN **************/
    // by ArbZ
function HTTP_Post($URL,$data, $referrer="") {
    // parsing the given URL
    $URL_Info=parse_url($URL);
    // Building referrer
    if($referrer=="") // if not given use this script as referrer
        $referrer=$_SERVER["SCRIPT_URI"];
    // making string from $data
    foreach($data as $key=>$value)
        $values[]="$key=".urlencode($value);
        $data_string=implode("&",$values);
    // Find out which port is needed - if not given use standard (=80)
    if(!isset($URL_Info["port"]))
        $URL_Info["port"]=80;
    // building POST-request: HTTP_HEADERs
    $request.="POST ".$URL_Info["path"]." HTTP/1.1\n";
    $request.="Host: ".$URL_Info["host"]."\n";
    $request.="Referer: $referer\n";
    $request.="Content-type: application/x-www-form-urlencoded\n";
    $request.="Content-length: ".strlen($data_string)."\n";
    $request.="Connection: close\n";
    $request.="\n";
    $request.=$data_string."\n";
    $fp = fsockopen($URL_Info["host"],$URL_Info["port"]);
    fputs($fp, $request);
    while(!feof($fp)) {
        $result .= fgets($fp, 128);
    }
    fclose($fp); //$eco = nl2br();
    function getTextBetweenTags($string, $tagname) {
        $pattern = "/<$tagname ?.*>(.*)<\/$tagname>/";
        preg_match($pattern, $string, $matches);
        return $matches[1];
    }
    //STORE THE FETCHED CONTENTS to a VARIABLE, because its way better and fast...
    $str = $result;
    $txt = getTextBetweenTags($str, "span"); $eco = $txt;  $result = explode("&",$result);
    return $result[1];
    <span style=background-color:LightYellow;color:blue>".trim($_GET['em'])."</span>
    </pre> "; 
}
</pre>
これが実際の例です。実行してから storage.txt を開いて、魔法のような結果を確認してください。
<?php
    function curlGet($target){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $target);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $result = curl_exec ($ch);
        curl_close ($ch);
        return $result;
    }
    // Its the next 3 lines that do the magic
    ignore_user_abort(true);
    header("Connection: close"); header("Content-Length: 0");
    echo str_repeat("s", 100000); flush();
    $i = $_GET['i'];
    if(!is_numeric($i)) $i = 1;
    if($i > 4) exit;
    if($i == 1) file_put_contents('storage.txt', '');
    file_put_contents('storage.txt', file_get_contents('storage.txt') . time() . "\n");
    sleep(5);
    curlGet($_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'] . '?i=' . ($i + 1));
    curlGet($_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'] . '?i=' . ($i + 1));
タイムアウトはミリ秒単位で設定できます。http: //www.php.net/manual/en/function.curl-setopt の「CURLOPT_CONNECTTIMEOUT_MS」を参照してください。