【问题标题】:How can I make this api post request in PHP?如何在 PHP 中发出这个 api 发布请求?
【发布时间】:2019-09-24 06:03:56
【问题描述】:

我使用 ajax 对 api 进行了发布请求。我想知道如何在 php 中做同样的事情。

<script type="text/javascript">
  var cbIntegrationId = "xxxxxx"; // I have it
  var clientId = "xxxxxxx"; //I have it
  var clientSecret = "xxxxx"; //I have it
  var tableName = "Test_Database";

    //Get access token
    $.post(
      "https://" + cbIntegrationId + ".caspio.com/oauth/token",
      {
        grant_type: "client_credentials",
        client_id: clientId,
        client_secret: clientSecret
      },
      function(cbAuth){
        //Run POST call
        $.ajax({
          url: "https://" + cbIntegrationId + ".caspio.com/rest/v2/tables/" + tableName + "/records?response=rows",
          type: 'POST',
          'data': JSON.stringify({"UniqueID":"988"}), //Define record values
          headers: {
            "Authorization": "Bearer " + cbAuth.access_token, //Extracts the access token from the initial authorization call
            "Content-Type": "application/json", //Required, otherwise 415 error is returned
            "Accept": "application/json"
          },
          dataType: 'json',
          success: function (data) {
            console.log(data.Result); //Check the console to view the new added row
          },
          error: function(data) {
            console.log(data.responseJSON); //Check the console to view error message if any
          }
        });
      }
    );
</script>

我做了一些研究,但找不到任何可以解决我的问题的方法。我真的需要你的帮助。

【问题讨论】:

标签: php ajax rest api


【解决方案1】:

您可以使用cURL 使用 PHP 调用 API。

因此,根据您的情况,您使用 POST 方法发送数据。所以,我们可以如下使用 cURL 和一些头文件,

$apiURL = "https://yourURL";
$uniqueID = "UniqueID:988";
$postData = json_encode($uniqueID); // Encode the data into a JSON string
$authorization = "Authorization: Bearer " . $token; // Prepare the authorization token

$curl = curl_init();

curl_setopt($curl, CURLOPT_HTTPHEADER, array($authorization, 'Content-Type: application/json', 'Accept : application/json')); // Inject the token into the header
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // To get actual result from the successful operation
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); // Specify HTTP protocol version to use;
curl_setopt($curl, CURLOPT_POST, 1); // Specify the request method as POST
curl_setopt($curl, CURLOPT_URL, $apiURL); // Pass the API URL
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData); // Set the posted fields
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // This will follow any redirects

$response = curl_exec($curl); // Here you will get the response after executing
$error = curl_error($curl); // Return a string containing the last error for the current session

curl_close($curl); // Close a cURL session

希望对您有所帮助!

【讨论】:

  • 我收到了这条消息Bad request
  • 谢谢@Hasitha M Jayawardana。有用。我的网址失败了
  • 很高兴知道它有帮助:)
  • 你好@Hasitha M Jayawardana。今天我想编辑一些字段。我怎样才能做到这一点?我尝试编辑您发送给我的代码,但没有成功
  • @AmílcarPaco 什么字段?如果您有其他问题,我建议您可以打开一个新线程。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-16
  • 1970-01-01
  • 2021-03-08
  • 1970-01-01
  • 2021-05-29
  • 2017-04-30
相关资源
最近更新 更多