0

(i've seen SOME version of this question so many times, hoping to make a thread with a comprihensive-ish list of answers)

3
  • This question does not permit to determine a correct answer... and the error handling is questionable. And if you want to establish a canonical answer, this is rather decided on meta. Commented Nov 26, 2020 at 16:42
  • @MartinZeitler well i want somewhere to point questions like this or this or this or this to next time i see them; Commented Nov 26, 2020 at 19:17
  • @MartinZeitler as for error handling, you're thinking about something like function ex_curl_setopt($ch,int $option, $val){if(!curl_setopt($ch,$option,$val)){throw new \RuntimeException("curl_setopt failed: ".curl_errno($ch).":".curl_error($ch));}} correct? Commented Nov 26, 2020 at 19:19

1 Answer 1

0

for example, what is the php-curl translation of:

curl -v https://api-m.sandbox.paypal.com/v1/oauth2/token \
  -H "Accept: application/json" \
  -H "Accept-Language: en_US" \
  -u "client_id:secret" \
  -d "grant_type=client_credentials"

-v translates to

curl_setopt($ch,CURLOPT_VERBOSE, 1);

PS! by default, curl sends this data to stderr, and stderr is usually visible when running curl from the terminal, but when running php-curl behind a webserver ala nginx/apache, it's not uncommon that stderr is linked to *the web-server's errorlog*, hence the VERBOSE log may arrive in the server error log, rather than the browser. a quickfix to this would be to set a custom CURLOPT_STDERR, ala:

$php_output_handle = fopen("php://output", "wb");
curl_setopt_array($ch, array(
    CURLOPT_VERBOSE => 1,
    CURLOPT_STDERR => $php_output_handle
));

but because of php garbage collection, when using this quickfix, keep in mind that it will break if php garabge collector closes $php_output_handle before the last curl_exec() call to the same handle.. it's usually not a problem, but it can happen.

.. moving on,

https://api-m.sandbox.paypal.com/v1/oauth2/token translates to:

curl_setopt($ch,CURLOPT_URL, 'https://api-m.sandbox.paypal.com/v1/oauth2/token');

and

-H "Accept: application/json" \
-H "Accept-Language: en_US" \

translates to

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Accept: application/json",
    "Accept-Language: en_US"
));

and -u "client_id:secret" translates to:

curl_setopt($ch,CURLOPT_USERPWD, "client_id:secret");

and -d "grant_type=client_credentials" (aka --data) translates to:

curl_setopt_array($ch, array(
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => http_build_query(array(
        "grant_type" => "client_credentials"
    ))
));

hence the full translation is:

$ch = curl_init();
curl_setopt_array($ch, array(
    CURLOPT_VERBOSE => 1,
    CURLOPT_URL => 'https://api-m.sandbox.paypal.com/v1/oauth2/token',
    CURLOPT_HTTPHEADER => array(
        "Accept: application/json",
        "Accept-Language: en_US"
    ),
    CURLOPT_USERPWD => 'client_id:secret',
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => http_build_query(array(
        "grant_type" => "client_credentials"
    ))
));
curl_exec($ch);

what is the translation of curl -F grant_type=client_credentials ? it's:

curl_setopt_array($ch, array(
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => array(
        "grant_type" => "client_credentials"
    )
));

what about uploading files, what's the translation of curl -F file=@file/path/to/upload.ext ? it's:

curl_setopt_array($ch, array(
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => array(
        "file" => new CURLFile("filepath/to/upload.ext")
    )
));

what's the translation of --location ? it's

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

how to upload JSON? like this:

curl_setopt_array($ch, array(
    CURLOPT_HTTPHEADER => array(
        "Content-Type: application/json"
    ),
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => json_encode(array(
        "whatever_key" => "whatever_data"
    ))
));

-X PUT translates to

curl_setopt($ch,CURLOPT_PUT,1);

as for --upload-file , there are several ways to do it, if you're dealing with small files which easily fits in ram, the easiest way to do it would be:

curl_setopt_array($ch, array(
    CURLOPT_PUT => 1,
    CURLOPT_POSTFIELDS => file_get_contents($file)
));

but if you need to support big files which you don't want to put in RAM,

$file = "file.ext";
$file_handle = fopen($file,"rb");
$file_size = filesize($file);
curl_setopt_array($ch, array(
    CURLOPT_UPLOAD => 1,
    CURLOPT_INFILESIZE=>$file_size,
    CURLOPT_INFILE=>$file_handle
));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.