Hỏi về CURL linux vs CURL trong php?

Em đang tìm hiểu về api và cách gọi api trên php . Em có xem tài liệu từ api mới đăng ký có mã CURL như sau :

curl -X POST "https://api.domain.io/v1/files/copy" \
-H 'Content-Type: application/json' \
-u your_private_key: -d '
{
	"sourceFilePath" : "/path/to/file.jpg",
	"destinationPath" : "/folder/to/copy/into/"
}
'

Nhờ mọi người trợ giúp dịch sang PHP giúp em . Em xin cảm ơn

Từ https://www.php.net/manual/en/curl.examples.php có nhiều ví dụ cho nhiều trường hợp sử dụng khác nhau

`Following code returns the curl output as a string.

<?php
        // create curl resource
        $ch = curl_init();

        // set url
        curl_setopt($ch, CURLOPT_URL, "example.com");

        //return the transfer as a string
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        // $output contains the output string
        $output = curl_exec($ch);

        // close curl resource to free up system resources
        curl_close($ch);      
?>`

Bạn có thể tìm hiểu thêm qua những keyword “php curl request authentication header API parameters

3 Likes

Với các Docs như này cứ bỏ vào Postman để chuyển qua ngôn ngữ khác hay debug.

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://api.domain.io/v1/files/copy',
  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 =>'
{
	"sourceFilePath" : "/path/to/file.jpg",
	"destinationPath" : "/folder/to/copy/into/"
}
',
  CURLOPT_HTTPHEADER => array(
    'Content-Type: application/json',
    'Authorization: Basic eW91cl9wcml2YXRlX2tleTo='
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

3 Likes
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?