2013-03-05 162 views
2

我將Badgeville REST API與我的PHP 5.3,curl 7.22應用程序集成在一起。使用PHP Curl與命令行curl的區別

BV的API文檔全部使用命令行卷曲調用作爲示例。當我運行這些示例時,它們工作正常。

當我試圖用PHP Curl類做同樣的事情時,我總是從BV服務器得到一個500錯誤。

我試圖在Chrome中使用高級Rest Rest Client擴展來執行同步功能。

PHP捲曲例子:

$ch = curl_init('http://sandbox.v2.badgeville.com/api/berlin/[private_api_key]/users.json'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3); 

if($this->getRequestType() == 'POST') 
{ 
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, 
     array(
      'user[name]' => 'Generic+Username', 
      'user[email]' => 'johndoe%40domainname.com' 
     ); 
    ); 
} 

$response = curl_exec($ch); 

REST客戶端,例如:

網址: http://sandbox.v2.badgeville.com/api/berlin/[private_api_key]/users。 JSON

POST 沒有頭部有效載荷:

用戶[名稱] =通用+用戶名&用戶[郵件] =爲johndoe%40domainname.com

我手動創建命令行卷曲電話,跑了與shell_exec(),但我真的不想有這樣做。

在我的研究中,我發現Drupal module和所有的API調用通過fsockopen()調用完成。

有沒有一些方法可以成功地使用PHP Curl類進行Badgeville調用?

回答

1

事實證明Badgeville有一個500錯誤,當一個curl請求進來時,它已經設置了頭文件。

返回錯誤代碼:

$ch = curl_init('http://sandbox.v2.badgeville.com/api/berlin/[private_api_key]/users.json'); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3); 

if($this->getRequestType() == 'POST') 
{ 
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, 
     array(
      'user[name]' => 'Generic+Username', 
      'user[email]' => 'johndoe%40domainname.com' 
     ); 
    ); 
} 

$response = curl_exec($ch); 

正常運轉的代碼:

$ch = curl_init('http://sandbox.v2.badgeville.com/api/berlin/[private_api_key]/users.json'); 
//curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3); 

if($this->getRequestType() == 'POST') 
{ 
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, 
     array(
      'user[name]' => 'Generic+Username', 
      'user[email]' => 'johndoe%40domainname.com' 
     ); 
    ); 
} 

$response = curl_exec($ch); 

SMH