【问题标题】:Make API request with cURL PHP使用 cURL PHP 发出 API 请求
【发布时间】:2022-10-24 10:14:25
【问题描述】:

我正在尝试连接到 API,这应该使用 cURL 完成。

这就是文档告诉我要发送的内容(尽管使用我自己的数据,这只是示例)。

curl --request POST \
  --url https://api.reepay.com/v1/subscription \
  --header 'Accept: application/json' \
  -u 'priv_11111111111111111111111111111111:' \
  --header 'Content-Type: application/json' \
  --data '{"plan":"plan-AAAAA",
           "handle": "subscription-101",
           "create_customer": {
              "handle": "customer-007",
              "email": "joe@example.com"
           },
           "signup_method":"link"}'

我尝试过的是这样,但我得到了错误:

$postdata = array();
    $postdata['plan'] = 'plan-AAAAA';
    $postdata['handle'] = 'subscription-101';
    $postdata['create_customer'] = ["handle" => "customer-007", "email" => "joe@example.com"];
    $postdata['signup_method'] = 'link';
    $cc =  curl_init();
    curl_setopt($cc,CURLOPT_POST,1);
    curl_setopt($cc,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($cc,CURLOPT_URL, "https://api.reepay.com/v1/subscription");
    curl_setopt($cc,CURLOPT_POSTFIELDS, $postdata);
    $result = curl_exec($cc);
    echo $result;

这是我得到的错误:{"error":"不支持的媒体类型","path":"/v1/subscription","timestamp":"2022-10-22T11:42:11.733+00:00","http_status":415,"http_reason ":"不支持的媒体类型"}

谁能帮我提出正确的要求?

【问题讨论】:

  • 该示例说,application/json 被接受,但您发布的是application/x-www-form-urlencoded。您需要对 postdata 进行 json_encode 并将其放入正文 + 设置适当的内容类型
  • 如果您不介意,我会将其作为答案发布,因此您可以接受;)
  • @HonkderHase 当然:-)

标签: php curl


【解决方案1】:

该示例说,application/json 被接受,但您发布的是 application/x-www-form-urlencoded。您需要对 postdata 进行 json_encode 并将其放入正文中 + 设置适当的内容类型。

为了更好,还要设置'Content-Length'......

$json_data = json_encode($postdata);
curl_setopt($cc, CURLOPT_POSTFIELDS, $json_data);
curl_setopt($cc, CURLOPT_HTTPHEADER, [
    'Content-Type: application/json',
    'Content-Length: '.strlen($json_data)
]);

【讨论】:

    【解决方案2】:

    根据您收到的错误,我想您需要将内容类型标头设置为 JSON。

    $curl = curl_init();
    
    curl_setopt_array($curl, array(
      CURLOPT_URL => 'https://api.reepay.com/v1/subscription',
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => '',
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 0,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => 'POST',
      CURLOPT_POSTFIELDS =>'{
        "plan": "plan-AAAAA",
        "handle": "subscription-101",
        "create_customer": {
            "handle": "customer-007",
            "email": "joe@example.com"
        },
        "signup_method": "link"
    }',
      CURLOPT_HTTPHEADER => array(
        'Accept: application/json',
        'Content-Type: application/json'
      ),
    ));
    
    $response = curl_exec($curl);
    
    curl_close($curl);
    echo $response;
    

    【讨论】:

      【解决方案3】:

      这应该有效:

      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, 'https://api.reepay.com/v1/subscription');
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
      curl_setopt($ch, CURLOPT_HTTPHEADER, [
          'Accept' => 'application/json',
          'Content-Type' => 'application/json',
      ]);
      curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
      curl_setopt($ch, CURLOPT_USERPWD, 'priv_11111111111111111111111111111111:');
      curl_setopt($ch, CURLOPT_POSTFIELDS, '{"plan":"plan-AAAAA",
                 "handle": "subscription-101",
                 "create_customer": {
                    "handle": "customer-007",
                    "email": "joe@example.com"
                 },
                 "signup_method":"link"}');
      
      $response = curl_exec($ch);
      
      curl_close($ch);
      

      【讨论】:

        猜你喜欢
        • 2012-02-29
        • 1970-01-01
        • 2023-03-06
        • 1970-01-01
        • 1970-01-01
        • 2010-11-03
        • 2013-12-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多