PHP 코드가 Amazon.com에서 쿼리를 보내어 책에 대한 정보를 검색합니다. 정보를 받으면 다음 프로그램을 실행하는 데 두 가지 가능성이 있습니다. 사용되어야 할 것을 정의 할 수있는 가능성은 연구를위한 총 도서 이익을 고려해야한다.PHP 코드의 실행을 중단하고 JavaScript로 신호를 보냅니다.
내가 지금하는 일은 첫 번째 요청을 보내고 총 결과 수를 검색하는 것입니다. 결과의 수에 따라 변수 $queryUrl
에 새 값을 할당합니다. 결과 수가 1200보다 큰 경우 프로그램을 실행해야합니다.
결과 수가 1200 개 미만인 경우 프로그램은 루프를 실행하여 결과의 전체 페이지와 나머지 PHP 코드를 반복하지만 단 한 번만 반복해야합니다.
현재 1200 개 미만의 결과가있는 경우. 프로그램은 결과의 모든 페이지를 거치지 만 PHP 코드의 끝에서 멈출 곳입니다. 쿼리의 매개 변수에 따라 코드를 여러 번 실행합니다. $searchMonthUrlParam
은 recupMonth
JavaScript를 상속합니다. 지금은
, 내가 가진 그
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;
자바 스크립트 :
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/>> <a href="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);
누군가가 간다 경우 stoper의 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);
}
그것은 확실히 사상가이다 : –
아니요 nodejs를 사용하지 않지만 스크립트가 끝나기 전에 반환되는 컨트롤러가 1200 개 이상인 경우 이미 발생합니다. 하지만 문제는 내가 현재 어떻게 보이는지 모른다는 것입니다. – mortiped
당신의 필요를 제대로 이해하지 못합니까? 결과가 1200보다 작 으면 어떻게 할 것입니까? 아마존을 다시 질의하거나 뭔가 다른 것을해야합니까? –