如何设置在响应值为空之前需要调用的 php curl 请求


How to set a php curl request that need to be called until the response value gets an empty

$myusername = "myusername";
$mypassword = "mypassword";
$site_url = "https:// my site url"; 
$cookiefile = "cookie_filename.txt";
$uagent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US....;
$postDatas = 'username='.$myusername.'&password='.$mypassword;
//----------1st request start---------
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$site_url);
curl_setopt($ch, CURLOPT_USERAGENT, $uagent);
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $postDatas); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$res = curl_exec($ch);
curl_close ($ch);
//----------1st request end---------
//----------2nd request start---------
$meee = explode("jsessionid=",str_replace("Content-Length:","",$res));
$sessId = explode(" ",trim($meee[1]));
$postDatas2 = $postDatas.'&jsessionid='.trim($sessId[0]);
 $headers_curl = array(
    "Accept-Encoding: .....",
    "Accept-Language: .....",
    "Accept: .....",
    "Set-Cookie: SESSIONIDS=".trim($sessId[0])."",
    "Path=....",
    "Cache-Control: max-age=0",
  );
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https:// my 2nd url");
curl_setopt($ch, CURLOPT_USERAGENT, $uagent);
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $postDatas2); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers_curl);
curl_setopt($ch, CURLOPT_REFERER, "https://my refer url");
curl_exec($ch);
//----------2nd request end--------
//----------3rd request start---------
curl_setopt($ch, CURLOPT_URL,"https:// my 3rd url");
curl_setopt($ch, CURLOPT_USERAGENT, $uagent);
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $postDatas2); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers_curl);
curl_setopt($ch, CURLOPT_REFERER, "https://......");
$checkResult = curl_exec($ch);
//----------3rd request end---------
preg_match_all('/<td width.*value="(.*)<'/form>/s',$checkResult,$chkData3);
preg_match_all('/value=.*?(.*)|">/',$chkData3[0][0],$checkKey);
$checkKey[1] = 
preg_replace('/[^a-zA-Z0-9_ %'[']'.'(')%&-]/s', '', $checkKey[1]);
//--------------end--------------------

(注意:根据我$checkResult的回答,我会得到$checkKey[1]
如果 $checkKey[1] 为空,则第 3 个 curl 请求必须停止
否则我想一次又一次地执行第 3 个请求,直到 $checkKey[1] 为空)

这样

的事情会有所帮助吗?

$checkKey[1] = "";
while (! empty($checkKey[1])) {
    // Execute request
    // And make your end
    preg_match_all('/<td width.*value="(.*)<'/form>/s',$checkResult,$chkData3);
    preg_match_all('/value=.*?(.*)|">/',$chkData3[0][0],$checkKey);
    $checkKey[1] = 
    preg_replace('/[^a-zA-Z0-9_ %'[']'.'(')%&-]/s', '', $checkKey[1]);
}
do { 
    $checkResult = "";
    $chkData3 = "";
    $checkKey = "";
    //----------3rd request start---------
    curl_setopt($ch, CURLOPT_URL,"https:// my 3rd url");
    curl_setopt($ch, CURLOPT_USERAGENT, $uagent);
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postDatas2); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers_curl);
    curl_setopt($ch, CURLOPT_REFERER, "https://......");
    $checkResult = curl_exec($ch);
    preg_match_all('/<td width.*value="(.*)<'/form>/s',$checkResult,$chkData3);
    preg_match_all('/value=.*?(.*)|">/',$chkData3[0][0],$checkKey);
    $checkKey[1] = preg_replace('/[^a-zA-Z0-9_ %'[']'.'(')%&-]/s', '', $checkKey[1]);
//----------3rd request end---------
} while (isset($checkKey[1]) && $checkKey[1] == "checkagain");

(注意:我刚刚使用了这个,它工作得很好,谢谢大家...