2012-02-29 114 views
0
while($dataR = mysql_fetch_array($data)){ 
    $postcode = str_replace(" ", "+", $dataR['Postcode']); 
    echo $postcode."<br />"; 
    $oPostcode = $dataR['Postcode']; 
    // Retrieve the DOM from a given URL 
    $url = 'http://www.1.com'; 
    $fields = array(
       'txtPostCode'=>urlencode($oPostcode) 


      ); 

    //url-ify the data for the POST 
    foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; } 
    rtrim($fields_string,'&'); 

    $ch = curl_init(); 

    //set the url, number of POST vars, POST data 
    curl_setopt($ch,CURLOPT_URL,$url); 
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 
    curl_setopt($ch,CURLOPT_POST,count($fields)); 
    curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string); 

    //execute post 
    $result = curl_exec($ch); 
    curl_close($ch); 
    unset($ch); 
    //close connection 

    $html = str_get_html($result); 
    print_r($html); 
} 

這就是我的代碼。然而,cURL部分僅在第一次運行 - 我必須做什麼?我試圖瞭解curl_multi_exec但找不到簡單的答案。cURL在PHP while循環只運行一次

+1

可能重複的:http://stackoverflow.com/questions/4517842/php-curl-loop-post-buffer-not-clearing – seanbreeden 2012-02-29 11:43:19

回答

1

這將是最好分開從環路的捲曲的請求...... 所以這樣的事情會做..

while($dataR = mysql_fetch_array($data)){ 
$postcode = str_replace(" ", "+", $dataR['Postcode']); 
echo $postcode."<br />"; 
$oPostcode = $dataR['Postcode']; 
// Retrieve the DOM from a given URL 
$url = 'http://www.1.com'; 
$fields = array(
      'txtPostCode'=>urlencode($oPostcode) 


     ); 

//url-ify the data for the POST 
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; } 
rtrim($fields_string,'&'); 


// Execute youru cURL here. 
$data = array(
'url' => $url, 
'fiels' => count($fields), 
'field_string' => $fields_string 
); 
executecURL($data); 

$html = str_get_html($result); 
print_r($html); 
} 

function executecURL($data) { 
    $ch = curl_init(); 
    //set the url, number of POST vars, POST data 
    curl_setopt($ch,CURLOPT_URL,$data['url']); 
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 
    curl_setopt($ch,CURLOPT_POST,count($data['fields'])); 
    curl_setopt($ch,CURLOPT_POSTFIELDS,$data['fields_string']); 

    //execute post 
    $result = curl_exec($ch); 
    curl_close($ch); 
    unset($ch); 
    //close connection 
} 
0

嘗試未設置「婷$場 & $ field_string

 

unset($fields); 
unset($fields_string); 
 

之前重新使用它