PHP

2013-04-30 18 views
7

'da REST API'sini kullanarak Paypal ödemesi yapma Bu kod örneğinin PHP'nin CURL yapısına, özellikle de -d tanıtıcısına nasıl yerleştirileceği açık değildir.PHP

curl -v https://api.sandbox.paypal.com/v1/payments/payment \ 
-H 'Content-Type:application/json' \ 
-H 'Authorization:Bearer EOjEJigcsRhdOgD7_76lPfrr45UfuI43zzNzTktUk1MK' \ 
-d '{ 
    "intent":"sale", 
    "redirect_urls":{ 
    "return_url":"http://<return URL here>", 
    "cancel_url":"http://<cancel URL here>" 
    }, 
    "payer":{ 
    "payment_method":"paypal" 
    }, 
    "transactions":[ 
    { 
     "amount":{ 
     "total":"7.47", 
     "currency":"USD" 
     }, 
     "description":"This is the payment transaction description." 
    } 
    ] 
}' 

Aşağıdaki sınama çağrısını kullandım, ancak bunu yukarıdaki örneğe nasıl genişleteceğimi bilmiyorum.

curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token"); 
curl_setopt($ch, CURLOPT_HEADER, false); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_USERPWD, $clientId.":".$secret); 
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials"); 

$result = curl_exec($ch); 

if(empty($result))die("Error: No response."); 
else 
{ 
    $json = json_decode($result); 
    print_r($json->access_token); 
} 

cevap

11

Böyle bir şey deneyin, -d göndermek istediğiniz veri. özel üstbilgiler CURLOPT_HTTPHEADER ile ayarlanır.

$data = '{ 
    "intent":"sale", 
    "redirect_urls":{ 
    "return_url":"http://<return URL here>", 
    "cancel_url":"http://<cancel URL here>" 
    }, 
    "payer":{ 
    "payment_method":"paypal" 
    }, 
    "transactions":[ 
    { 
     "amount":{ 
     "total":"7.47", 
     "currency":"USD" 
     }, 
     "description":"This is the payment transaction description." 
    } 
    ] 
}'; 

curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/payments/payment"); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Content-Type: application/json", 
    "Authorization: Bearer EOjEJigcsRhdOgD7_76lPfrr45UfuI43zzNzTktUk1MK", 
    "Content-length: ".strlen($data)) 
); 

burada seçeneklerine bir göz, http://php.net/manual/en/function.curl-setopt.php Seti curl_setopt ($ ch, CURLOPT_HEADER, false) sahip olmak; örneğin, başlıklara ihtiyacınız yoksa.

+0

Bu tam bir çözüm mü? Cevap alamıyorum. İlk örnekte çok benzer bir koddan alınan kod ve erişim belirtecine yetkilendirmeyi değiştirdim. – RevNoah

+0

Hata veriyor: Yanıt yok. –

+0

Yanıt vermekte sadece 0 veriyor ... Lütfen kod için öner: – Shalini