2014-01-23 38 views
0

在PayPal教程making your first call上,它爲您提供了一個JSON字符串,我發現它無法發佈。我已經耗盡谷歌,我似乎無法弄清楚如何將這些數據發佈到貝寶。下面的代碼給了我一個415錯誤代碼(「不支持的媒體類型」)。我一直被困在這個錯誤上好幾個小時,而且越來越厭倦它。我會感謝任何能夠幫助我的人。我試圖讓我的描述清楚,以免在未來找到別人可以輕鬆找到的答案。通過PHP cURL將多維數組發佈到PayPal

 $post_data = array (
      'intent' => 'sale', 
      'redirect_urls' => array (
       'return_url' => 'http://example.com/return', 
       'cancel_url' => 'http://example.com/cancel' 
      ), 
      'payer' => array (
       'payment_mehod' => 'paypal' 
      ), 
      'transactions' => array (
       'amount' => array (
        'total' => '7.47', 
        'currency' => 'USD' 
       ) 
      ) 
     ); 

     $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/payments/payment"); 
     curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); 
     curl_setopt($ch, CURLOPT_HEADER, true); 
     curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: Bearer ' . $result_array['access_token']]); 
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
     curl_setopt($ch, CURLOPT_POST, true); 
     curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data)); 
     $result = curl_exec($ch); 

編輯:在第二次提供的代碼最後一行我也用http_build_query,我也試過只是簡單地發送它作爲一個多維數組。錯誤代碼415返回這兩個。

回答

0
curl_setopt($ch, CURLOPT_HTTPHEADER, 
    array(
    'Content-Type: application/json', 
    'Authorization: Bearer ' . $result_array['access_token'] 
    ) 
); 
+0

我嚇壞了愛你。非常感謝。我是個白癡! – makynopa