Hướng dẫn php call api post json - php call api post json

Bạn cần sử dụng thư viện Curl để gửi yêu cầu này.

 'blogger#post',
    'blog' => array('id' => $blogID),
    'title' => 'A new post',
    'content' => 'With exciting content...'
);

// Setup cURL
$ch = curl_init('https://www.googleapis.com/blogger/v3/blogs/'.$blogID.'/posts/');
curl_setopt_array($ch, array(
    CURLOPT_POST => TRUE,
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_HTTPHEADER => array(
        'Authorization: '.$authToken,
        'Content-Type: application/json'
    ),
    CURLOPT_POSTFIELDS => json_encode($postData)
));

// Send the request
$response = curl_exec($ch);

// Check for errors
if($response === FALSE){
    die(curl_error($ch));
}

// Decode the response
$responseData = json_decode($response, TRUE);

// Close the cURL handler
curl_close($ch);

// Print the date from the response
echo $responseData['published'];

Nếu, vì một số lý do, bạn không thể/không muốn sử dụng Curl, bạn có thể làm điều này:

 'blogger#post',
    'blog' => array('id' => $blogID),
    'title' => 'A new post',
    'content' => 'With exciting content...'
);

// Create the context for the request
$context = stream_context_create(array(
    'http' => array(
        // http://www.php.net/manual/en/context.http.php
        'method' => 'POST',
        'header' => "Authorization: {$authToken}\r\n".
            "Content-Type: application/json\r\n",
        'content' => json_encode($postData)
    )
));

// Send the request
$response = file_get_contents('https://www.googleapis.com/blogger/v3/blogs/'.$blogID.'/posts/', FALSE, $context);

// Check for errors
if($response === FALSE){
    die('Error');
}

// Decode the response
$responseData = json_decode($response, TRUE);

// Print the date from the response
echo $responseData['published'];

JSON là định dạng dữ liệu phổ biến nhất để trao đổi dữ liệu giữa trình duyệt và máy chủ. Định dạng dữ liệu JSON chủ yếu được sử dụng trong các dịch vụ web để trao đổi dữ liệu thông qua API. Khi bạn làm việc với các dịch vụ web và API, việc gửi dữ liệu JSON qua yêu cầu POST là chức năng cần thiết nhất. PHP Curl giúp bạn dễ dàng đăng dữ liệu JSON lên URL. Trong hướng dẫn này, chúng tôi sẽ chỉ cho bạn cách đăng dữ liệu JSON bằng Php Curl và nhận dữ liệu JSON trong PHP. is the most popular data format for exchanging data between a browser and a server. The JSON data format is mostly used in web services to interchange data through API. When you working with web services and APIs, sending JSON data via POST request is the most required functionality. PHP cURL makes it easy to POST JSON data to URL. In this tutorial, we will show you how to POST JSON data using PHP cURL and get JSON data in PHP.

Gửi dữ liệu JSON qua bài đăng với PHP Curl

Ví dụ sau đây thực hiện yêu cầu bài HTTP và gửi dữ liệu JSON đến URL với Curl trong PHP.cURL in PHP.

  • Chỉ định URL ($url) trong đó dữ liệu JSON được gửi.
  • Bắt đầu tài nguyên Curl mới bằng Curl_init ().curl_init().
  • Thiết lập dữ liệu trong mảng PHP và mã hóa thành chuỗi JSON bằng json_encode ().json_encode().
  • Gắn dữ liệu JSON vào các trường bài bằng cách sử dụng tùy chọn Curlopt_PostFields.
  • Đặt loại nội dung của yêu cầu thành application/json bằng tùy chọn Curlopt_httpheader.
  • Trả về phản hồi dưới dạng chuỗi thay vì xuất nó bằng tùy chọn Curlopt_returnTransfer.
  • Cuối cùng, hàm curl_exec () được sử dụng để thực hiện yêu cầu POST.curl_exec() function is used to execute the POST request.
// API URL
$url 'http://www.example.com/api';// Create a new cURL resource
$ch curl_init($url);// Setup request to send json via POST
$data = array(
    
'username' => 'codexworld',
    
'password' => '123456'
);
$payload json_encode(array("user" => $data));// Attach encoded JSON string to the POST fields
curl_setopt($chCURLOPT_POSTFIELDS$payload);// Set the content type to application/json
curl_setopt($chCURLOPT_HTTPHEADER, array('Content-Type:application/json'));// Return response instead of outputting
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);// Execute the POST request
$result curl_exec($ch);// Close cURL resource
curl_close($ch);

Nhận dữ liệu bài đăng JSON bằng PHP

Ví dụ sau đây cho thấy cách bạn có thể nhận hoặc lấy dữ liệu bài JSON bằng PHP.

  • Sử dụng hàm json_decode () để giải mã dữ liệu JSON trong PHP.json_decode() function to decoded JSON data in PHP.
  • Hàm file_get_contents () được sử dụng để nhận dữ liệu ở định dạng dễ đọc hơn.file_get_contents() function is used to received data in a more readable format.
$data json_decode(file_get_contents('php://input'), true);

Bạn có muốn nhận trợ giúp thực hiện hoặc sửa đổi hoặc tăng cường chức năng của tập lệnh này không? Gửi yêu cầu dịch vụ trả phí

Nếu bạn có bất kỳ câu hỏi nào về kịch bản này, hãy gửi nó cho cộng đồng QA của chúng tôi - đặt câu hỏi

API Post Post trong PHP như thế nào?

Để thực hiện yêu cầu POST đến điểm cuối API, bạn cần gửi yêu cầu POST HTTP đến máy chủ và chỉ định tiêu đề yêu cầu loại nội dung chỉ định loại phương tiện dữ liệu trong phần thân của yêu cầu POST. Tiêu đề độ dài nội dung cho biết kích thước của dữ liệu trong phần thân của yêu cầu POST.send an HTTP POST request to the server and specify a Content-Type request header that specifies the data media type in the body of the POST request. The Content-Length header indicates the size of the data in the body of the POST request.

Làm thế nào vượt qua đối tượng JSON trong yêu cầu POST trong PHP?

Gửi dữ liệu JSON qua POST với PHP Curl Chỉ định URL ($ url) nơi dữ liệu JSON được gửi.Bắt đầu tài nguyên Curl mới bằng Curl_init ().Thiết lập dữ liệu trong mảng PHP và mã hóa thành chuỗi JSON bằng json_encode ().Gắn dữ liệu JSON vào các trường bài bằng cách sử dụng tùy chọn Curlopt_PostFields.Specify the URL ( $url ) where the JSON data to be sent. Initiate new cURL resource using curl_init(). Setup data in PHP array and encode into a JSON string using json_encode(). Attach JSON data to the POST fields using the CURLOPT_POSTFIELDS option.

Làm cách nào để gửi yêu cầu bài viết với cơ thể JSON?

2.1.Tạo một đối tượng URL.....
2.2.Mở một kết nối.....
2.3.Đặt phương thức yêu cầu.....
2.4.Đặt tham số tiêu đề loại nội dung yêu cầu.....
2.5.Đặt loại định dạng phản hồi.....
2.6.Đảm bảo kết nối sẽ được sử dụng để gửi nội dung.....
2.7.Tạo cơ thể yêu cầu.....

Làm cách nào để nhận dữ liệu được mã hóa JSON trong PHP?

Để nhận chuỗi JSON, chúng tôi có thể sử dụng php: // nhập vào cùng với hàm file_get_contents () giúp chúng tôi nhận dữ liệu JSON dưới dạng tệp và đọc nó thành một chuỗi.Sau đó, chúng ta có thể sử dụng hàm json_decode () để giải mã chuỗi JSON.use the “php://input” along with the function file_get_contents() which helps us receive JSON data as a file and read it into a string. Later, we can use the json_decode() function to decode the JSON string.