PHP中有没有办法进行异步HTTP调用?我不关心响应,我只想做类似的事情file_get_contents(),但是在执行其余代码之前不要等待请求完成.这对于在我的应用程序中引发排序的"事件"或触发长进程非常有用.
有任何想法吗?
我的脚本由服务器调用.从服务器我会收到ID_OF_MESSAGE和TEXT_OF_MESSAGE.
在我的脚本中,我将处理传入的文本并使用params生成响应:ANSWER_TO_ID和RESPONSE_MESSAGE.
问题是我正在向incomming发送响应"ID_OF_MESSAGE",但是在收到http响应200之后,向我发送消息的服务器将把他的消息设置为发送给我(这意味着我可以发送给他的响应).
解决方案之一是将消息保存到数据库并制作一些将每分钟运行的cron,但我需要立即生成响应消息.
是否有一些解决方案如何发送到服务器http响应200而不是继续执行PHP脚本?
非常感谢
我正在尝试创建一个PHP脚本,我已经完成了脚本,但是它需要10分钟才能完成它的设计过程.这不是问题,但我认为我必须一直保持页面加载这很烦人.我可以拥有它以便我启动该过程然后在10分钟后返回并查看它生成的日志文件吗?
有些操作需要花费太多时间,导致ajax请求超时.
如何首先完成对请求的响应,然后继续该操作?
我正在尝试使用Slack Custom命令,并且不太确定如何使用延迟消息,因为Yoda Speak External API需要3秒以上的响应时间.
我做了以下事情:
/Yoda在我的情况下发送了slack命令并收到了reponse_url.post对响应URL 使用以下内容.Run Code Online (Sandbox Code Playgroud)$data_string = '{"response_type": "in_channel", "text":"Checking,please wait..."}' ; $chs = curl_init(); curl_setopt($chs, CURLOPT_URL, $response_url); curl_setopt($chs, CURLOPT_POST, true); curl_setopt($chs, CURLOPT_POSTFIELDS, $data_string); curl_setopt($chs, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($chs, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($chs, CURLOPT_RETURNTRANSFER, true); curl_setopt($chs, CURLOPT_POST, 1); curl_setopt($chs, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); $results = curl_exec($chs);
Run Code Online (Sandbox Code Playgroud)$chsres = curl_init(); curl_setopt($chsres, CURLOPT_URL, "https://yoda.p.mashape.com/yoda?sentence=welcome+to+stack"); curl_setopt($chsres, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($chsres, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($chsres, CURLOPT_VERBOSE, true); curl_setopt($chsres, CURLOPT_TIMEOUT, 45); curl_setopt($chsres, CURLOPT_RETURNTRANSFER, true); curl_setopt($chsres, CURLOPT_HTTPHEADER, array('Content-Type:application/json', "X-Mashape-Key:> …
我在 CodeIgniter - Ubuntu Server 上运行我的代码。
我一直在研究运行函数的异步方法。
这是我的功能:
<?php
// Registers a new keyword for prod to the DB.
public function add_keyword() {
$keyword_p = $this->input->post('key_word');
$prod = $this->input->post('prod_name');
$prod = $this->kas_model->search_prod_name($prod);
$prod = $prod[0]->prod_id;
$country = $this->input->post('key_country');
$keyword = explode(", ", $keyword_p);
var_dump($keyword);
$keyword_count = count($keyword);
echo "the keyword count: $keyword_count";
// problematic part that needs forking
for ($i=0; $i < $keyword_count ; $i++) {
// get new vars from $keyword_count
// run API functions to get new data_arrays …Run Code Online (Sandbox Code Playgroud) 我想知道是否可以立即发送 HTTP 响应并继续执行脚本。
背景:在我向服务器提出的所有请求中,有一个创建了 Excel 文件(使用 PHPSpreadSheet),因为创建此文件可能需要更长的时间,我正在考虑向客户端响应HTTP 202 状态代码,例如:
header("HTTP/1.1 202 Excel file in process");
并在每次状态代码到达时在主 js 文件上编写一个侦听器,以激活时间间隔 ( setInterval()) 并每隔一定秒数询问 Excel 文件是否准备就绪
$(document).ajaxSuccess(function ( event, xhr, settings) {
if(xhr.status === 202) {
//Activate the setInterval function to ask the server every 10 seconds whether the file is ready or not
}
});
Run Code Online (Sandbox Code Playgroud)
(我知道我必须激活/停用间隔)
因此,每当我收到有关 createExcel.php 文件的请愿书时,我想立即回复用户已收到请愿书并开始制作此类 Excel 文件
就像是:
<?php
//Tell user that the petition has being received
header("HTTP/1.1 202 Excel file in process");
//Kill this …Run Code Online (Sandbox Code Playgroud) 客户端如何向服务器发送请求以触发执行,服务器立即发送响应然后执行请求的代码?与此类似:
echo 'the execution is starting';
exit;
execution_function(); // <--- is it possible to this function being executed after exit
Run Code Online (Sandbox Code Playgroud)
我希望响应立即生效,以便客户端在服务器执行请求时可以执行其他操作.
php ×9
asynchronous ×2
http ×2
timeout ×2
ajax ×1
codeigniter ×1
curl ×1
fork ×1
javascript ×1
laravel ×1
laravel-5 ×1
queue ×1
response ×1
slack ×1
slack-api ×1