我想使用PHP cURL發佈到REST服務,但我遇到了一些困難(這是我從來沒有使用cURL之前!!)。cURL POST到REST服務
我已經把這段代碼:
<?php
error_reporting(E_ALL);
if ($result == "00")
{
$url = 'http://127.0.0.1/xxxxxx/AccountCreator.ashx'; /*I've tried it a combination of ways just to see which might work */
$curl_post_data = array(
'companyName' =>urlencode($companyName),
'mainContact' =>urlencode($mainContact),
'telephone1' =>urlencode($telephone1),
'email' => urlencode($email),
'contact2' => urlencode($contact2),
'telephone2' => urlencode($telephone2)
'email2' => urlencode($email2);
'package' => urlencode($package)
);
foreach($curl_post_data as $key=>$value) {$fields_string .=$key. '=' .$value.'&';
}
rtrim($fields_string, '&');
die("Test: ".$fields_string);
$ch = curl_init();
curl_setopt ($ch, CURLOPT, $url);
curl_setopt ($ch, CURLOPT_POST, count($curl_post_data));
curl_setopt ($ch, CURLOPT_POSTFIELDS, $fields_string);
$result = curl_exec($ch);
curl_close($ch);
在此之後,我的代碼發送電子郵件,並執行IF語句。我知道這工作正常,當我嘗試插入此cURL請求時,我纔開始遇到麻煩。
我試過這個,但它不運行。就像我跟支付合作夥伴整合,它只是說:
Your transaction has been successful but there was a problem connecting back to the merchant's web site. Please contact the merchant and advise them that you received this error message. Thank you.
時收到的確切的錯誤是HTTP 500錯誤。
謝謝。這裏
'電子郵件'=>進行urlencode($電子郵件), 'contact2'=>進行urlencode($ contact2) 'telephone2'=>進行urlencode($ telephone2) 'EMAIL2'=>進行urlencode($ EMAIL2); 'package'=> urlencode($ package)你應該真的修復這個數組!它丟失了逗號,還有一個分號..這一切都搞砸了 – sathia 2010-09-27 10:07:39
@sathia,謝謝,我修正了這個問題。尷尬我錯過了!模具(「Test:」。$ fields_string);正在輸出:: Test:companyName = xxx&mainContact = xxx + xxx&telephone1 = xxxx,其中包含所有正確的值。然而,這是所有輸出 - 它不會更新我的數據庫,它不會繼續處理以下mail()函數或IF語句。 – 109221793 2010-09-27 10:28:56
當然,現在你需要評論一下die(),因爲die不知道死的是什麼:殺死程序的執行。只是評論它/ /死(「測試..它會繼續 – sathia 2010-09-27 10:36:44