Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert command line cURL to PHP cURL

Tags:

php

curl

I've never done any curl before so am in need of some help. I've tried to work this out from examples but cannot get my head around it!

I have a curl command that I can successfully run from a linux(ubuntu) command line that puts a file to a wiki through an api.

I would need to incorporate this curl command in a PHP script I'm building.

How can I translate this curl command so that it works in a PHP script?

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

cookie.txt contains the authentication but I don't have a problem putting this in clear text in the script as this will be run by me only.

@test.png must be a variable such as $filename

http://hostname/@api/deki/pages/=TestPage/files/= must be a variable such as $pageurl

Thanks for any help.

like image 934
Brigante Avatar asked Dec 21 '09 11:12

Brigante


People also ask

What can you do with curl to PHP converter online?

What can you do with Curl to PHP Converter Online? Curl to PHP is very unique tool to convert curl command to http request of PHP. The input provide by the user's curl command to generate PHP Code. This tool saves your time and helps to generate PHP code with ease.

What is curl command in Linux?

Curl (short for Client URL) is a command-line tool created by Daniel Stenberg that allows you to send data to a server using many different types of protocols. Curl is based on the libcurl and currently supports HTTP, HTTPS, FTP, and SFTP.

How do I make a curl request on GitHub?

2) Ctrl-click a request. 3) Click "Copy" > "Copy as cURL". 4) Paste it in the curl command box above. This also works in Safari and Firefox. GitHub is matching all contributions to this project on Github Sponsors . This is a surprisingly good deal and I'm not sure how long it's going to last.

How do I copy and paste a curl in GitHub?

2) Ctrl-click a request. 3) Click "Copy" > "Copy as cURL". 4) Paste it in the curl command box above. This also works in Safari and Firefox. GitHub is matching all contributions to this project on Github Sponsors .


8 Answers

a starting point:

<?php

$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

...
?>

See also: http://www.php.net/manual/en/function.curl-setopt.php

like image 112
miku Avatar answered Oct 02 '22 05:10

miku


You need ...

curl-to-PHP : https://incarnate.github.io/curl-to-php/

"Instantly convert curl commands to PHP code"

like image 28
kris Avatar answered Oct 03 '22 05:10

kris


Whicvhever cURL you have in command line, you can convert it to PHP with this tool:

https://incarnate.github.io/curl-to-php/

It helped me after long long hours of searching for a solution! Hope it will help you out too! Your solution is this:

// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "http://hostname/@api/deki/pages/=TestPage/files/=test.png");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$post = array(
    "file" => "@" .realpath("test.png")
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");


$headers = array();
$headers[] = "Content-Type: image/png";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
like image 29
Vladimir Despotovic Avatar answered Oct 03 '22 05:10

Vladimir Despotovic


This is an autogenerated list of which curl commandline options might map onto which php CURLOPT_ constant:

--arg description curl_setopt()
-a
--append
Append to target file when uploading CURLOPT_FTPAPPEND, CURLOPT_APPEND
--basic Use HTTP Basic Authentication CURLAUTH_BASIC
--cacert <file> CA certificate to verify peer against CURLE_SSL_CACERT, CURLE_SSL_CACERT_BADFILE
--capath <dir> CA directory to verify peer against CURLOPT_CAPATH
-E
--cert <certificate[:password]>
Client certificate file and password CURLOPT_SSLCERT, CURLOPT_SSLCERTPASSWD, CURLOPT_SSLCERTTYPE, CURLE_SSL_CACERT, CURLE_SSL_CERTPROBLEM, CURLE_SSL_PEER_CERTIFICATE, CURLE_SSL_CACERT_BADFILE, CURLOPT_ISSUERCERT, CURLINFO_CERTINFO, CURLOPT_CERTINFO
--connect-to <HOST1:PORT1:HOST2:PORT2> Connect to host CURLOPT_CONNECT_TO
-b
--cookie <data>
Send cookies from string/file CURLOPT_COOKIE, CURLOPT_COOKIEFILE, CURLOPT_COOKIEJAR, CURLOPT_COOKIESESSION, CURL_LOCK_DATA_COOKIE, CURLINFO_COOKIELIST, CURLOPT_COOKIELIST
--crlf Convert LF to CRLF in upload CURLOPT_CRLF, CURLOPT_CRLFILE
--crlfile <file> Get a CRL list in PEM format from the given file CURLOPT_CRLFILE
-d
--data <data>
HTTP POST data CURLOPT_READDATA, CURL_LOCK_DATA_COOKIE, CURL_LOCK_DATA_DNS, CURL_LOCK_DATA_SSL_SESSION
--delegation <LEVEL> GSS-API delegation permission CURLGSSAPI_DELEGATION_FLAG, CURLGSSAPI_DELEGATION_POLICY_FLAG, CURLOPT_GSSAPI_DELEGATION
--digest Use HTTP Digest Authentication CURLAUTH_DIGEST, CURLAUTH_DIGEST_IE
--dns-interface <interface> Interface to use for DNS requests CURLOPT_DNS_INTERFACE
--dns-servers <addresses> DNS server addrs to use CURLOPT_DNS_SERVERS
--engine <name> Crypto engine to use CURLOPT_SSLENGINE, CURLOPT_SSLENGINE_DEFAULT, CURLE_SSL_ENGINE_NOTFOUND, CURLE_SSL_ENGINE_SETFAILED, CURLINFO_SSL_ENGINES
-f
--fail
Fail silently (no output at all) on HTTP errors CURLOPT_FAILONERROR, CURLE_FAILED_INIT, CURLE_FTP_PORT_FAILED, CURLE_HTTP_PORT_FAILED, CURLE_LDAP_SEARCH_FAILED, CURLE_SSL_ENGINE_SETFAILED, CURLE_FTP_SSL_FAILED, CURL_FNMATCHFUNC_FAIL
-F
--form <name=content>
Specify multipart MIME data CURLE_FTP_WEIRD_227_FORMAT, CURLE_MALFORMAT_USER, CURLE_URL_MALFORMAT, CURLE_URL_MALFORMAT_USER, CURLM_CALL_MULTI_PERFORM
--ftp-account <data> Account data string CURLOPT_FTP_ACCOUNT
--ftp-alternative-to-user <command> String to replace USER [name] CURLOPT_FTP_ALTERNATIVE_TO_USER
-P
--ftp-port <address>
Use PORT instead of PASV CURLE_FTP_PORT_FAILED
--ftp-skip-pasv-ip Skip the IP address for PASV CURLOPT_FTP_SKIP_PASV_IP
--ftp-ssl-ccc Send CCC after authenticating CURLOPT_FTP_SSL_CCC
-G
--get
Put the post data in the URL and use GET CURLOPT_HTTPGET, CURLE_FTP_CANT_GET_HOST, CURLE_FTP_COULDNT_GET_SIZE, CURL_RTSPREQ_GET_PARAMETER
-I
--head
Show document info only CURLOPT_HEADER, CURLOPT_HEADERFUNCTION, CURLOPT_HTTPHEADER, CURLOPT_WRITEHEADER, CURLINFO_HEADER_OUT, CURLINFO_HEADER_SIZE, CURLHEADER_SEPARATE, CURLHEADER_UNIFIED, CURLOPT_HEADEROPT, CURLOPT_PROXYHEADER
-H
--header <header/@file>
Pass custom header(s) to server CURLOPT_HEADER, CURLOPT_HEADERFUNCTION, CURLOPT_HTTPHEADER, CURLOPT_WRITEHEADER, CURLINFO_HEADER_OUT, CURLINFO_HEADER_SIZE, CURLHEADER_SEPARATE, CURLHEADER_UNIFIED, CURLOPT_HEADEROPT, CURLOPT_PROXYHEADER
--http2 Use HTTP 2 CURLOPT_HTTP200ALIASES, CURL_VERSION_HTTP2
--ignore-content-length Ignore the size of the remote resource CURLOPT_IGNORE_CONTENT_LENGTH
--interface <name> Use network INTERFACE (or address) CURLOPT_INTERFACE, CURLOPT_DNS_INTERFACE
-6
--ipv6
Resolve names to IPv6 addresses CURL_VERSION_IPV6
--key <key> Private key file name CURLOPT_SSLKEY, CURLOPT_SSLKEYPASSWD, CURLOPT_SSLKEYTYPE, CURLE_SSL_PINNEDPUBKEYNOTMATCH, CURLOPT_SSH_PRIVATE_KEYFILE, CURLOPT_SSH_PUBLIC_KEYFILE, CURLOPT_SSH_HOST_PUBLIC_KEY_MD5, CURLOPT_KEYPASSWD, CURLSSH_AUTH_KEYBOARD, CURLSSH_AUTH_PUBLICKEY, CURLOPT_PINNEDPUBLICKEY
--krb <level> Enable Kerberos with security CURLOPT_KRB4LEVEL, CURLOPT_KRBLEVEL
--local-port <num/range> Force use of RANGE for local port numbers CURLINFO_LOCAL_PORT
-L
--location
Follow redirects CURLOPT_FOLLOWLOCATION
--login-options <options> Server login options CURLOPT_LOGIN_OPTIONS
--mail-auth <address> Originator address of the original email CURLOPT_MAIL_AUTH
--mail-from <address> Mail from this address CURLOPT_MAIL_FROM
--mail-rcpt <address> Mail to this address CURLOPT_MAIL_RCPT
--negotiate Use HTTP Negotiate (SPNEGO) authentication CURLAUTH_GSSNEGOTIATE, CURLAUTH_NEGOTIATE
-n
--netrc
Must read .netrc for user name and password CURLOPT_NETRC, CURL_NETRC_IGNORED, CURL_NETRC_OPTIONAL, CURL_NETRC_REQUIRED, CURLOPT_NETRC_FILE
--netrc-file <filename> Specify FILE for netrc CURLOPT_NETRC_FILE
--netrc-optional Use either .netrc or URL CURL_NETRC_OPTIONAL
--noproxy <no-proxy-list> List of hosts which do not use proxy CURLOPT_NOPROXY
--ntlm Use HTTP NTLM authentication CURLAUTH_NTLM, CURLAUTH_NTLM_WB
--ntlm-wb Use HTTP NTLM authentication with winbind CURLAUTH_NTLM_WB
--oauth2-bearer <token> OAuth 2 Bearer Token CURLOPT_XOAUTH2_BEARER
--pass <phrase> Pass phrase for the private key CURLOPT_SSLCERTPASSWD, CURLOPT_SSLKEYPASSWD, CURLE_BAD_PASSWORD_ENTERED, CURLE_FTP_USER_PASSWORD_INCORRECT, CURLE_FTP_WEIRD_PASS_REPLY, CURLFTPSSL_CCC_PASSIVE, CURLOPT_KEYPASSWD, CURLSSH_AUTH_PASSWORD, CURLOPT_PASSWORD, CURLOPT_PROXYPASSWORD, CURLOPT_TLSAUTH_PASSWORD
--path-as-is Do not squash .. sequences in URL path CURLOPT_PATH_AS_IS
--pinnedpubkey <hashes> FILE/HASHES Public key to verify peer against CURLE_SSL_PINNEDPUBKEYNOTMATCH
--proto <protocols> Enable/disable PROTOCOLS CURLE_UNSUPPORTED_PROTOCOL, CURLOPT_PROTOCOLS, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_ALL, CURLPROTO_DICT, CURLPROTO_FILE, CURLPROTO_FTP, CURLPROTO_FTPS, CURLPROTO_HTTP, CURLPROTO_HTTPS, CURLPROTO_LDAP, CURLPROTO_LDAPS, CURLPROTO_SCP, CURLPROTO_SFTP, CURLPROTO_TELNET, CURLPROTO_TFTP, CURLPROTO_IMAP, CURLPROTO_IMAPS, CURLPROTO_POP3, CURLPROTO_POP3S, CURLPROTO_RTSP, CURLPROTO_SMTP, CURLPROTO_SMTPS, CURLPROTO_RTMP, CURLPROTO_RTMPE, CURLPROTO_RTMPS, CURLPROTO_RTMPT, CURLPROTO_RTMPTE, CURLPROTO_RTMPTS, CURLPROTO_GOPHER, CURLPROTO_SMB, CURLPROTO_SMBS, CURLOPT_DEFAULT_PROTOCOL
--proxy-service-name <name> SPNEGO proxy service name CURLOPT_PROXY_SERVICE_NAME
-p
--proxytunnel
Operate through an HTTP proxy tunnel (using CONNECT) CURLOPT_HTTPPROXYTUNNEL
--pubkey <key> SSH Public key file name CURLE_SSL_PINNEDPUBKEYNOTMATCH
-Q
--quote
Send command(s) to server before transfer CURLOPT_POSTQUOTE, CURLOPT_PREQUOTE, CURLOPT_QUOTE, CURLE_FTP_QUOTE_ERROR
--random-file <file> File for reading random data from CURLOPT_RANDOM_FILE
-r
--range <range>
Retrieve only the bytes within RANGE CURLOPT_RANGE, CURLE_HTTP_RANGE_ERROR, CURLOPT_LOCALPORTRANGE
-e
--referer <URL>
Referrer URL CURLOPT_AUTOREFERER, CURLOPT_REFERER
-X
--request <command>
Specify request command to use CURLOPT_CUSTOMREQUEST, CURLINFO_REQUEST_SIZE, CURLOPT_RTSP_REQUEST
--resolve <host:port:address[,address]...> Resolve the host+port to this address CURLE_COULDNT_RESOLVE_HOST, CURLE_COULDNT_RESOLVE_PROXY, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4, CURL_IPRESOLVE_V6, CURL_IPRESOLVE_WHATEVER, CURLOPT_RESOLVE
--retry <num> Retry request if transient problems occur CURLFTP_CREATE_DIR_RETRY
--sasl-ir Enable initial response in SASL authentication CURLOPT_SASL_IR
--service-name <name> SPNEGO service name CURLOPT_PROXY_SERVICE_NAME, CURLOPT_SERVICE_NAME
--socks4 <host[:port]> SOCKS4 proxy on given host + port CURLPROXY_SOCKS4, CURLPROXY_SOCKS4A
--socks4a <host[:port]> SOCKS4a proxy on given host + port CURLPROXY_SOCKS4A
--socks5 <host[:port]> SOCKS5 proxy on given host + port CURLPROXY_SOCKS5, CURLPROXY_SOCKS5_HOSTNAME, CURLOPT_SOCKS5_GSSAPI_NEC, CURLOPT_SOCKS5_GSSAPI_SERVICE
--socks5-gssapi Enable GSS-API auth for SOCKS5 proxies CURLOPT_SOCKS5_GSSAPI_NEC, CURLOPT_SOCKS5_GSSAPI_SERVICE
--socks5-gssapi-nec Compatibility with NEC SOCKS5 server CURLOPT_SOCKS5_GSSAPI_NEC
--socks5-gssapi-service <name> SOCKS5 proxy service name for GSS-API CURLOPT_SOCKS5_GSSAPI_SERVICE
--socks5-hostname <host[:port]> SOCKS5 proxy, pass host name to proxy CURLPROXY_SOCKS5_HOSTNAME
-Y
--speed-limit <speed>
Stop transfers slower than this CURLOPT_LOW_SPEED_LIMIT
-y
--speed-time <seconds>
Trigger 'speed-limit' abort after this time CURLOPT_LOW_SPEED_TIME
--ssl Try SSL/TLS CURLOPT_SSLCERT, CURLOPT_SSLCERTPASSWD, CURLOPT_SSLCERTTYPE, CURLOPT_SSLENGINE, CURLOPT_SSLENGINE_DEFAULT, CURLOPT_SSLKEY, CURLOPT_SSLKEYPASSWD, CURLOPT_SSLKEYTYPE, CURLOPT_SSLVERSION, CURLOPT_SSL_CIPHER_LIST, CURLOPT_SSL_VERIFYHOST, CURLOPT_SSL_VERIFYPEER, CURLE_SSL_CACERT, CURLE_SSL_CERTPROBLEM, CURLE_SSL_CIPHER, CURLE_SSL_CONNECT_ERROR, CURLE_SSL_ENGINE_NOTFOUND, CURLE_SSL_ENGINE_SETFAILED, CURLE_SSL_PEER_CERTIFICATE, CURLE_SSL_PINNEDPUBKEYNOTMATCH, CURLINFO_SSL_VERIFYRESULT, CURL_LOCK_DATA_SSL_SESSION, CURL_SSLVERSION_DEFAULT, CURL_SSLVERSION_SSLv2, CURL_SSLVERSION_SSLv3, CURL_SSLVERSION_TLSv1, CURL_VERSION_SSL, CURLE_FTP_SSL_FAILED, CURLFTPSSL_ALL, CURLFTPSSL_CONTROL, CURLFTPSSL_NONE, CURLFTPSSL_TRY, CURLOPT_FTP_SSL, CURLFTPAUTH_SSL, CURLOPT_FTPSSLAUTH, CURLINFO_SSL_ENGINES, CURLE_SSL_CACERT_BADFILE, CURLOPT_SSL_SESSIONID_CACHE, CURLOPT_FTP_SSL_CCC, CURLFTPSSL_CCC_ACTIVE, CURLFTPSSL_CCC_NONE, CURLFTPSSL_CCC_PASSIVE, CURLOPT_USE_SSL, CURLUSESSL_ALL, CURLUSESSL_CONTROL, CURLUSESSL_NONE, CURLUSESSL_TRY, CURLOPT_SSL_OPTIONS, CURLSSLOPT_ALLOW_BEAST, CURL_SSLVERSION_TLSv1_0, CURL_SSLVERSION_TLSv1_1, CURL_SSLVERSION_TLSv1_2, CURLOPT_SSL_ENABLE_ALPN, CURLOPT_SSL_ENABLE_NPN, CURLOPT_SSL_VERIFYSTATUS, CURLOPT_SSL_FALSESTART, CURLSSLOPT_NO_REVOKE
-2
--sslv2
Use SSLv2 CURL_SSLVERSION_SSLv2
-3
--sslv3
Use SSLv3 CURL_SSLVERSION_SSLv3
--stderr Where to redirect stderr CURLOPT_STDERR
--tcp-fastopen Use TCP Fast Open CURLOPT_TCP_FASTOPEN
--tcp-nodelay Use the TCP_NODELAY option CURLOPT_TCP_NODELAY
-t
--telnet-option <opt=val>
Set telnet option CURLE_TELNET_OPTION_SYNTAX, CURLE_UNKNOWN_TELNET_OPTION
--tftp-blksize <value> Set TFTP BLKSIZE option CURLOPT_TFTP_BLKSIZE
--tftp-no-options Do not send any TFTP options CURLOPT_TFTP_NO_OPTIONS
-1
--tlsv1
Use TLSv1.0 or greater CURL_SSLVERSION_TLSv1, CURL_SSLVERSION_TLSv1_0, CURL_SSLVERSION_TLSv1_1, CURL_SSLVERSION_TLSv1_2
--unix-socket <path> Connect through this Unix domain socket CURLOPT_UNIX_SOCKET_PATH
--url <url> URL to work with CURLOPT_URL
-u
--user <user:password>
Server user and password CURLOPT_PROXYUSERPWD, CURLOPT_USERAGENT, CURLOPT_USERPWD, CURLE_FTP_USER_PASSWORD_INCORRECT, CURLE_FTP_WEIRD_USER_REPLY, CURLE_MALFORMAT_USER, CURLE_URL_MALFORMAT_USER, CURLOPT_FTP_ALTERNATIVE_TO_USER, CURLOPT_PROXYUSERNAME, CURLOPT_USERNAME, CURLOPT_TLSAUTH_USERNAME
-v
--verbose
Make the operation more talkative CURLOPT_VERBOSE
-V
--version
Show version number and quit CURLOPT_HTTP_VERSION, CURLOPT_SSLVERSION, CURLVERSION_NOW, CURL_HTTP_VERSION_1_0, CURL_HTTP_VERSION_1_1, CURL_HTTP_VERSION_NONE, CURL_SSLVERSION_DEFAULT, CURL_SSLVERSION_SSLv2, CURL_SSLVERSION_SSLv3, CURL_SSLVERSION_TLSv1, CURL_VERSION_IPV6, CURL_VERSION_KERBEROS4, CURL_VERSION_LIBZ, CURL_VERSION_SSL, CURL_HTTP_VERSION_2_0, CURL_VERSION_HTTP2, CURL_SSLVERSION_TLSv1_0, CURL_SSLVERSION_TLSv1_1, CURL_SSLVERSION_TLSv1_2, CURL_HTTP_VERSION_2, CURL_HTTP_VERSION_2TLS, CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE

Note that this only lists somewhat exact matches of --long options to similarly named CURLOPT_ constants. But it should give you enough hints on how to compare the curl --help output and the PHP curl_setopt() list.

like image 45
2 revs Avatar answered Oct 03 '22 05:10

2 revs


Try this:

$cmd='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';
exec($cmd,$result);
like image 42
manny Avatar answered Oct 03 '22 05:10

manny


the --libcurl option was added for this purpose, even though it makes a C program I think it should be fairly easy to translate to PHP

like image 28
Daniel Stenberg Avatar answered Oct 04 '22 05:10

Daniel Stenberg


Using MYYN's answer as a starting point, and this page as a reference on how to send POST data using PHP cURL, here is my suggestion (I am working on something very similar at the moment):

<?php

$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

$post = array("$filename"=>"@$filename");
curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
$response = curl_exec($ch);
?>

You can probably optimize the many curl_setopts with the use of a curl_setopt_array() call if you desire.

like image 38
r00fus Avatar answered Oct 05 '22 05:10

r00fus


Better this. In one line.

$cmd='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';
exec($cmd,$result);
like image 1
Toxicity Avatar answered Oct 01 '22 05:10

Toxicity