如何停止执行PHP代码并将信号发送到js?

我的PHP代码在Amazon.com上发送查询以检索有关书籍的信息。

当它收到信息时,有两种可能性可以执行以下程序。定义应该使用的内容的可能性,必须查看用于研究的账面利润总数。

目前,我正在发送第一个请求并检索结果总数。根据结果​​的数量,我为变量$queryUrl分配了一个新值。如果结果数大于1200,则程序将按原样执行。

如果结果数少于1200,则程序应完成执行循环,以遍历结果的整个页面以及php代码的其余部分(仅一次)。

目前,如果结果少于1200。该程序遍历结果的所有页面,但在PHP代码结尾处停下来。它会根据查询的参数多次执行所有代码,而$searchMonthUrlParam会继承JavaScript变量recupMonth

现在,我有

的PHP:

//Retrieve variable value passed in POST from JavaScript

$pageNum = (isset($_POST["pageNum"]) && $_POST["pageNum"]) ? intval($_POST["pageNum"]) : 1;

$writeMode = (isset($_POST["writeMode"]) && $_POST["writeMode"]) ? $_POST["writeMode"] : "w";

$searchType = (isset($_POST["searchType"]) && $_POST["searchType"]) ? intval($_POST["searchType"]) : 0;

$month = (isset($_POST["month"]) && $_POST["month"]) ? intval($_POST["month"]) : date("n");

$year = (isset($_POST["year"]) && $_POST["year"]) ? intval($_POST["year"]) : date("Y") ;

$keyword = (isset($_POST["keyword"]) && strlen($_POST["keyword"])) ? $_POST["keyword"] : "";

$startMonth = (isset($_POST["startMonth"]) && strlen($_POST["startMonth"])) ? $_POST["startMonth"] : NULL;

$startYear = (isset($_POST["startYear"]) && strlen($_POST["startYear"])) ? $_POST["startYear"] : NULL;

$endMonth = (isset($_POST["endMonth"]) && strlen($_POST["endMonth"])) ? $_POST["endMonth"] : NULL;

$endYear = (isset($_POST["endYear"]) && strlen($_POST["endYear"])) ? $_POST["endYear"] : NULL;

if($keyword) {

if($writeMode === "w") {

file_put_contents(CSV_FILE, "");

}

$searchMonthUrlParam = "&field-datemod=".$month;

$searchYearUrlParam = "&field-dateyear=".$year;

$searchTypeUrlParam = "";

switch($searchType) {

case SEARCH_TYPE_TITLE:

$searchTypeUrlParam = "&field-title=";

break;

case SEARCH_TYPE_KEYWORDS:

$searchTypeUrlParam = "&field-keywords=";

break;

case SEARCH_TYPE_AUTHOR:

$searchTypeUrlParam = "&field-author=";

$searchTypeUrlParam = "&field-publisher=";

break;

case SEARCH_TYPE_PUBLISHER:

break;

}

//send request to Amazon

$queryUrl = AMAZON_TOTAL_BOOKS_COUNT . $searchMonthUrlParam . $searchYearUrlParam . $searchTypeUrlParam . urlencode($keyword) . '&page=' . $pageNum;

$queryResult = file_get_contents($queryUrl);

//Search number total results

if (preg_match('/of\s+([0-9,]+)\s+Results/', $queryResult, $matches)) {

$totalResults = (int) str_replace(',', '', $matches[1]);

} else {

throw new \RuntimeException('Total number of results not found');

}

//this condition work

if ($totalResults > MAX_RESULT_ALL_PAGES) {

$queryUrl = AMAZON_SEARCH_URL . $searchMonthUrlParam . $searchYearUrlParam . $searchTypeUrlParam . urlencode($keyword) . '&page=' . $pageNum;

}

//with this condition I don't know how to proceed

else {

$queryUrl = AMAZON_TOTAL_BOOKS_COUNT.$searchMonthUrlParam.$searchYearUrlParam.$searchTypeUrlParam.urlencode($keyword)."&page=".$pageNum;

}

$htmlResultPage = file_get_html($queryUrl);

$htmlQueryResult = $htmlResultPage->find("div[class=result]");

exit;

JavaScript:

if(processedResultCount === 0) {

pageNum = 1;

recupMonth--;

if(recupMonth === 0 && recupYear > endYear) {

recupMonth = 12;

recupYear--;

}

else if(parseInt(recupMonth, 10) === parseInt(endMonth, 10) && parseInt(recupYear, 10) === parseInt(endYear, 10)) {

alert("Processing finished");

if(totalResultCount != 0) {

contentElt.innerHTML = "Total processed results: " + totalResultCount + '<br/><br/>&gt; <a href="https://www.coder.work/article/703722/amazon_keyword_stats.csv" title="Download CSV result file">Download CSV result file</a>';

}

return;

}

}

getAmazonResult(pageNum, writeMode);

return;

}

}

}

xmlHttp.open("POST", "ctrl/getAmazonResult.php", true);

xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");

xmlHttp.send("pageNum=" + pageNum + "&writeMode=" + writeMode + "&searchType=" + searchType + "&month=" + recupMonth + "&year=" + recupYear + "&keyword=" + keyword + "&startMonth=" + startMonth + "&startYear=" + startYear + "&endMonth=" + endMonth + "&endYear=" + endYear);

有人将在其他方面提供解决方案,以解决如何提高停止器php代码的执行力,但他完成一次完整执行一次的能力吗?

最佳答案

要退出循环并结束PHP执行,只需返回一个结果:

//this is the condition you indicated works

if ($totalResults > MAX_RESULT_ALL_PAGES) {

$queryUrl = AMAZON_SEARCH_URL . $searchMonthUrlParam . $searchYearUrlParam . $searchTypeUrlParam . urlencode($keyword) . '&page=' . $pageNum;

}

//this is the condition you indicated does not work

else {

return someSortOfResultProcessing($queryResult);

}

以上是 如何停止执行PHP代码并将信号发送到js? 的全部内容, 来源链接: www.h5w3.com/122482.html

回到顶部