2014-09-02 191 views
2

我需要使用PHP捲曲從我的網站將數據發佈到ASP網站發佈的數據,以ASP站點。如何使用PHP捲曲

的ASP網站是http://www.hotfrog.com.au/AddYourBusinessSingle.aspx

對於這點我走近像下面

  1. 使用PHP捲曲保持餅乾&會議

  2. 從爬着從該網站的網頁的HTML源源ASP隱變量值提取

  3. 準備好的帖子字符串與所需的表單字段

  4. 並將該數據發佈到使用PHP curl的目標ASP站點url,但響應是頁面表單信息沒有條目詳細信息,甚至沒有顯示來自curl響應的非條目字段的驗證消息。

  5. 對於該過程保持相同的值CURLOPT_USERAGENT,CURLOPT_COOKIEJAR,CURLOPT_COOKIEFILE

的ASP網站所需的表單字段這樣下面

ctl00 $ contentSection $ CompanyDetailsControl $ txtBusinessName

ctl00 $ contentSection $ CompanyDetailsControl $ txtSuburb

是他們發佈直接或張貼有由PHP目標站點之前需要這些字段名稱的任何編碼捲曲

任何人都可以有這種解決方案或PHP捲曲的任何其他方法對ASP網站解決方案?

回答

1

使用

function get_headers_from_curl_response($headerContent) { 

    $headers = []; 

    // Split the string on every "double" new line. 
    $arrRequests = explode("\r\n\r\n", $headerContent); 

    // Loop of response headers. The "count() -1" is to 
    //avoid an empty row for the extra line break before the body of the esponse. 
    for ($index = 0; $index < count($arrRequests) - 1; $index++) { 

     foreach (explode("\r\n", $arrRequests[$index]) as $i => $line) { 
      if ($i === 0) { 
       $headers[$index]['http_code'] = $line; 
      } 
      else { 
       list ($key, $value) = explode(': ', $line); 
       $headers[$index][$key] = $value; 
      } 
     } 
    } 

    return $headers; 
} 

function regexExtract($text, $regex, $regs, $nthValue) { 
    if (preg_match($regex, $text, $regs)) { 
     $result = $regs[$nthValue]; 
    } 
    else { 
     $result = ""; 
    } 

    return $result; 
} 

$regexViewstate = '/__VIEWSTATE\" value=\"(.*)\"/i'; 
$regexEventVal = '/__EVENTVALIDATION\" value=\"(.*)\"/i'; 

$ch = curl_init("__YOUR__URL__HERE__"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_HEADER, 1); 
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt'); 

$response = curl_exec($ch); 
curl_close($ch); 

$viewstate = regexExtract($response, $regexViewstate, $regs, 1); 
$eventval = regexExtract($response, $regexEventVal, $regs, 1); 

$params = [ 
    '__EVENTTARGET'  => '', 
    '__EVENTARGUMENT'  => '', 
    '__VIEWSTATE'   => $viewstate, 
    '__EVENTVALIDATION' => $eventval, 
    'ctl00%24txtUsername' => 'xxx', 
    'ctl00%24txtPassword' => 'xxx', 
    'ctl00$ImgLogin.x' => '0', 
    'ctl00$ImgLogin.y' => '0',]; 

$ch2 = curl_init("http://gsc.klub-modul.dk/cms/ShowContentPage.aspx?ContentPageID=1"); 
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch2, CURLOPT_HEADER, 1); 
curl_setopt($ch2, CURLOPT_POST, true); 
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch2, CURLOPT_POSTFIELDS, http_build_query($params)); 
curl_setopt($ch2, CURLOPT_COOKIE, 'cookies.txt'); 
curl_setopt($ch2, CURLOPT_COOKIEJAR, 'cookies2.txt'); 

$response2 = curl_exec($ch2); 
curl_close($ch2); 

foreach (get_headers_from_curl_response($response2) as $value) { 
    foreach ($value as $key => $value2) { 
     echo $key.": ".$value2."<br />"; 
    } 
}