2010-03-12 64 views
-5

如何將下面的PHP代碼轉換爲VB.NET?如何將下面的PHP代碼轉換爲VB.NET?

<?php 
    $X_HOST ="foo.com"; 
    $X_URL = "/index.php"; 
    $X_PORT ="8080"; 
    $X_USERNAME = "foo"; 
    $X_PASSWORD = "bar"; 
    $s_POST_DATA = "Channel=UK.VODAFONE"; // Channel 
    $s_POST_DATA .= "&Shortcode=12345"; // Shortcode 
    $s_POST_DATA .= "&SourceReference=3456"; // Source Reference 
    $s_POST_DATA .= "&MSISDN=447811111111"; // Phone 
    $s_POST_DATA .= "&Content=test"; // Content 
    $s_POST_DATA .= "&DataType=0"; // Data Type 
    $s_POST_DATA .= "&Premium=1"; // Premium 
    $s_POST_DATA .= "&CampaignID=4321"; // CampaignID 
    $s_Request = "POST ".$X_URL." HTTP/1.0\r\n"; 
    $s_Request .="Host: ".$X_HOST.":".$X_PORT."\r\n"; 
    $s_Request .="Authorization: Basic ".base64_encode($X_USERNAME.":".$X_PASSWORD)."\r\n"; 
    $s_Request .="Content-Type: application/x-www-form-urlencoded\r\n"; 
    $s_Request .="Content-Length: ".strlen($s_POST_DATA)."\r\n"; 
    $s_Request .="\r\n".$s_POST_DATA; 
    //Sends out the request to the server. 
    $fp = fsockopen ($X_HOST, $X_PORT, $errno, $errstr, 30) or die("Error!!!"); 
    fputs ($fp, $s_Request); 
    while (!feof($fp)) { 
    $s_GatewayResponse .= fgets ($fp, 128); 
    } 
    fclose ($fp); 
    //Array of official response codes. 
    $a_Responses = array(
    "100" => "Server has returned an unspecified error.", 
    "101" => "Server successfully received the request.", 
    "102" => "Server has returned an database error", 
    "103" => "Server has returned an syntax error." 
    ); 
    echo "<HTML>\n<BODY>\n\n"; 
    //Checks for an official response code. 
    foreach ($a_Responses as $s_ResponseCode => $s_ResponseDescription) { 
    if (stristr($s_GatewayResponse, "\n$s_ResponseCode\n")) { 
    echo "A response code of $s_ResponseCode was returned – "; 
    echo $s_ResponseDescription"; 
    $b_CodeReturned = true; 
    } 
    } 
    //Checks for an authorization failure where an official response code has 
    //not been recognized. 
    if (!$b_CodeReturned) { 
    if (stristr($s_GatewayResponse, "HTTP/1.1 401")) { 
    echo "The server rejected your username/password (HTTP 401)."; 
    } else { 
    echo "No recognised response code was returned by the server."; 
    } 
    } 
    echo "\n\n</BODY>\n</HTML>"; 
    ?> 

<?php 
    $s_ref = $HTTP_POST_VARS["Reference"]; // Reference 
    $s_trg = $HTTP_POST_VARS["Trigger"]; // trigger 
    $s_shc = $HTTP_POST_VARS["Shortcode"]; // shortcode 
    $s_pho = $HTTP_POST_VARS["MSISDN"]; // MSISDN 
    $s_con = $HTTP_POST_VARS["Content"]; // Content 
    $s_chn = $HTTP_POST_VARS["Channel"]; // Channel 
    $s_pay = $HTTP_POST_VARS["DataType"]; // Data Type 
    $s_dat = $HTTP_POST_VARS["DateReceived"]; // Date Received 
    $s_cam = $HTTP_POST_VARS["CampaignID"]; // CampaignID 
    $b_IsValid = getValidateRequest($s_ref, $s_trg, $s_shc, $s_pho, $s_con, $s_cam, $s_chn, $s_pay, 
    $s_dat); 
    if ($b_IsValid) 
    { 
    $s_ResponseCode = "success"; 
    } 
    else 
    { 
    $s_ResponseCode = "fail"; 
    } 
    exit($s_ResponseCode); 
    /*******************************************************************************/ 
    function getValidateRequest ($s_req_ref, $s_req_trg, $s_req_shc, $s_req_pho, $s_req_con, $s_req_cam, 
    $s_req_chn, $s_req_pay, $s_req_dat) { 
    /* 
    * Stub function to be replaced with whatever process is needed to 
    * process/validate request from server by specific client requirements. 
    */ 
    return(true); 
    } 
    ?> 

最後

<?php 
$s_ref = $HTTP_POST_VARS["Reference"]; // Reference 
$s_sta = $HTTP_POST_VARS["Status"]; // Status 
$s_dat = $HTTP_POST_VARS["DateDelivered"]; // Date Delivered 
$b_IsValid = getValidateReceipt($s_ref, $s_sta, $s_dat); 
if ($b_IsValid) 
{ 
$s_ResponseCode = "success"; 
} 
else 
{ 
$s_ResponseCode = "fail"; 
} 
exit($s_ResponseCode); 
/*******************************************************************************/ 
function getValidateReceipt ($s_req_ref, $s_req_sta, $s_req_dat) 
{ 
/* 
* Stub function to be replaced with whatever process is needed to 
* process/validate receipts from server by specific client requirements. 
*/ 
return(true); 
} 
?> 

非常感謝你提前 問候 格雷格

+0

沒有人會爲您寫下大量的代碼。 *你不明白什麼? – 2010-03-12 12:41:57

+0

該代碼是很容易理解,如果你知道任何語言... – rogeriopvl 2010-03-12 12:45:50

+0

對不起,我不知道PHP – Fet 2010-03-12 12:47:14

回答

5

第一批代碼只是下載具有特定標頭&認證的網頁。查看HttpWebRequest課程。

其他兩個代碼塊只是從客戶端retreiving POST'd數據,然後它將這些變量傳遞給getValidateReceipt()函數驗證發送的內容,但它註釋它是空的,所以它什麼都不做。

這看起來像使用CURL庫和$ _POST舊的PHP代碼將是更現代的做事方式。

+0

謝謝Klinky! – Fet 2010-03-12 14:11:41

5

瞭解VB.NET,然後開始翻譯的代碼,仔細,逐行。它應該是非常簡單的。

如果您不知道VB.NET中的PHP函數的等價物,請查看它或詢問SO。

或者:查看/詢問VB.NET預製解決方案,這些解決方案與腳本具有相同的目標。

0

This爲我工作。

它基本上是每種語言的常見編程概念與運行敘述並行比較。

我認爲這是學習如何將PHP轉換爲vb.net的最佳途徑