duaner1634 2017-01-05 03:29
浏览 66
已采纳

如何使用PHP发布curl

I am using an API in which CURL is required for use.

Here is the code:

    <?

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"
}'


?>

Please provide an exact conversion of the CURL code to make it work within PHP.

  • 写回答

1条回答 默认 最新

  • dpevsxjn809817 2017-01-05 06:40
    关注

    an equivalent PHP curl call would be

    <?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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?