2017-01-05 99 views
-2

我正在使用需要使用CURL的API。如何使用PHP發佈curl

下面是代碼:

<? 

curl -v -X POST https://sandbox.bluesnap.com/services/2/transactions \ 
-H 'Content-Type: application/json' \ 
-H 'Accept: application/json' \ 
-H 'Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=' \ 
-d ' 


{ 
"amount": 11, 
"recurringTransaction": "ECOMMERCE", 
"merchantTransactionId": 3, 
"softDescriptor": "DescTest", 
"cardHolderInfo": { 
    "firstName": "test first name", 
    "lastName": "test last name" 
}, 
"currency": "GBP", 
"creditCard": { 
    "expirationYear": 2018, 
    "securityCode": 837, 
    "expirationMonth": "02", 
    "cardNumber": 4263982640269299 
}, 
"cardTransactionType": "AUTH_CAPTURE" 
}' 


?> 

請提供捲曲代碼的精確轉換,使其在PHP工作。

+0

什麼叫 「讓這段代碼的工作」 是什麼意思? – emaillenin

+0

你不能只在任何代碼周圍放置PHP標籤,並且它是PHP。看看手冊。如果您打算使用'<?'打開,也啓用短標籤。 http://php.net/manual/en/book.curl.php – chris85

+0

@emaillenin我的意思是我怎樣才能使PHP文件中的CURL工作。我的意思是如何將它轉換爲PHP友好 –

回答

0

等效PHP捲曲通話將

<?php 
$data=array(
"amount"=>11, 
"recurringTransaction"=>"ECOMMERCE", 
"merchantTransactionId"=>3, 
"softDescriptor"=>"DescTest", 
"cardHolderInfo"=>array(
    "firstName"=>"test first name", 
    "lastName"=>"test last name" 
), 
"currency"=>"GBP", 
"creditCard"=>array(
    "expirationYear"=>2018, 
    "securityCode"=>837, 
    "expirationMonth"=>"02", 
    "cardNumber"=>4263982640269299 
), 
"cardTransactionType"=>"AUTH_CAPTURE" 
); 
$data_json=json_encode($data,JSON_BIGINT_AS_STRING | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); 
$ch=curl_init(); 
curl_setopt_array($ch,array(
CURLOPT_VERBOSE=>true, 
CURLOPT_POST=>true, 
CURLOPT_URL=>'https://sandbox.bluesnap.com/services/2/transactions', 
CURLOPT_HTTPHEADER=>array(
'Content-Type: application/json', 
'Accept: application/json', 
'Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=', 
), 
CURLOPT_POSTFIELDS=>$data_json, 
CURLOPT_USERAGENT=>'curl/7.50.1', 
)); 
curl_exec($ch); 
curl_close($ch); 

enter image description here