Home > Backend Development > PHP Tutorial > How Can I Translate This cURL Command into a PHP cURL Script?

How Can I Translate This cURL Command into a PHP cURL Script?

Patricia Arquette
Release: 2024-12-03 08:39:10
Original
945 people have browsed it

How Can I Translate This cURL Command into a PHP cURL Script?

Translating cURL Command Line to PHP cURL

Seeking assistance in converting a command-line cURL command to its corresponding PHP script, a user presents the following challenge:

curl -b cookie.txt -X PUT \
     --data-binary "@test.png" \
     -H "Content-Type: image/png" \    
     "http://hostname/@api/deki/pages/=TestPage/files/=test.png" \
     -0
Copy after login

The goal is to incorporate this command into a PHP script, with the following variables:

  • $filename for @test.png
  • $pageurl for http://hostname/@api/deki/pages/=TestPage/files/=

PHP Script Conversion:

To translate this command line cURL to a PHP script, one can start with the following code:

$pageurl = "http://hostname/@api/deki/pages/=TestPage/files/=";
$filename = "test.png";

$theurl = $pageurl . $filename;

$ch = curl_init($theurl);
curl_setopt($ch, CURLOPT_COOKIE, ...); // -b
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); // -X
curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE); // --data-binary
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: image/png']); // -H
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); // -0

...
Copy after login

Additional details regarding specific options can be obtained from the PHP manual: http://www.php.net/manual/en/function.curl-setopt.php

The above is the detailed content of How Can I Translate This cURL Command into a PHP cURL Script?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template