2010-09-30 43 views
1

我試圖將此PHP cURL函數轉換爲與我的rails應用程序一起工作。這段代碼來自需要驗證POST參數的SMS支付網關。由於我是一個大的PHP noob,我不知道如何處理這個問題。在rails應用程序中編寫cURL like函數

$verify_url = 'http://smsgatewayadress'; 



    $fields = ''; 

    $d = array(

      'merchant_ID' => $_POST['merchant_ID'], 

      'local_ID' => $_POST['local_ID'], 

      'total' => $_POST['total'], 

      'ipn_verify' => $_POST['ipn_verify'], 

      'timeout' => 10, 
      ); 



    foreach ($d as $k => $v) 

    { 

     $fields .= $k . "=" . urlencode($v) . "&"; 

    } 

    $fields = substr($fields, 0, strlen($fields)-1); 



    $ch = curl_init($verify_url); //this initiates a HTTP connection to $verify_url, the connection headers will be stored in $ch 

    curl_setopt($ch, CURLOPT_POST, 1); //sets the delivery method as POST 

    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); //The data that is being sent via POST. From what I can see the cURL lib sends them as a string that is built in the foreach loop above 

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); //This verifies if the target url sends a redirect header and if it does cURL follows that link 

    curl_setopt($ch, CURLOPT_HEADER, 0); //This ignores the headers from the answer 

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //This specifies that the curl_exec function below must return the result to the accesed URL 

    $result = curl_exec($ch); //It ransfers the data via POST to the URL, it gets read and returns the result 



    if ($result == true) 

    { 

     //confirmed 

     $can_download = true; 

    } 

    else 

    { 

     //failed 
     $can_download = false; 

    } 

} 



if (strpos($_SERVER['REQUEST_URI'], 'ipn.php')) 

    echo $can_download ? '1' : '0'; //we tell the sms sever that we processed the request 

我GOOGLE了Rails中的捲曲LIB對方,發現一噸的選項,但沒有,我能理解,並在此腳本執行同樣的方式使用。

如果任何人都可以幫我把這個腳本從PHP轉換爲紅寶石,將不勝感激。

回答

2

最直接的方法可能是使用Ruby庫,該庫是cURL最直接的包裝器。 Curl::Easy中的很多選項直接映射到您在此處的內容。基礎可能是:

url = "http://smsgatewayadress/" 
Curl::Easy.http_post(url, 
    Curl::PostField.content('merchant_ID', params[:merchant_ID]), 
    # ... 
) 
+0

嘿!非常感謝您的快速回答。我現在正在查看Curb文檔,並嘗試重寫相應的ruby腳本。除了'CURLOPT_RETURNTRANSFER'之外,我發現所有參數都等價,但我仍在搜索中。一旦完成,我會在這裏發佈腳本,也許其他人會需要它。 – v3rt1go 2010-09-30 15:16:06

相關問題