php - 为什么这个 php 代码显示 json 数组而没有代码来显示它?

标签 php json paypal

<?php

function pay()
{
        $data = '
    {
          "intent":"sale",

          "redirect_urls":
          {
            "return_url":"http://n1.lchs-is.org",
            "cancel_url":"http://n1.lchs-is.org"
          },

          "payer":
          {
            "payment_method":"paypal"
          },

          "transactions":
          [
            {
              "amount":
              {
                "total":"7.47",
                "currency":"USD"
              },

              "description":"This is the payment transaction description."
            }
          ]
    }
        ';


    //Start curl request
    $ch = curl_init();

    //Set curl options
    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 A101.lTxNCRX3B-vhQIN4991wdthA6SzCtPKganKXVOZta84hADs-25S5OGk4yyG-HLzR.lM-rDOIJAaEXGWhcYi2YF8yhnRy",
        ));

    //Display results
    $json = curl_exec($ch); // <------- It's displaying this array that it receives from curl right here
    curl_close($ch);
}
pay();

?>

我编写了这段 PHP 代码来简单地创建一个 paypal 支付,当我运行它时,它显示 JSON 数组,即使我没有编写它。为什么会这样,我该如何解决?

此外,我在执行 pay() 函数后键入的任何代码都不会执行。例如,如果我输入 echo "test"; 那行代码不会被执行。

最佳答案

您可能需要设置 CURLOPT_RETURNTRANSFER

例如

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // Store value in string $response below
$response = curl_exec($curl); //execute and get the results

关于php - 为什么这个 php 代码显示 json 数组而没有代码来显示它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33443681/

相关文章:

paypal - 除了 paypal 之外,Web 应用程序还应该支持哪些其他支付服务?

php - fatal error :调用未定义的方法 mysqli::error()

PHP mysql_num_rows() 返回错误

php - firephp 输出中的彩色点是什么意思?

php - 注册流程+php mysql

php - 使用phonegap提交表单数据和文件?

iphone - iPhone 设备上的 Paypal SDK 支持

javascript - 将输入值分配给变量

java - JSONObject 使用单例

c# - PayPal SDK 未预填充的账单字段