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.
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
Answer:
You need …
curl-to-PHP : https://incarnate.github.io/curl-to-php/
“Instantly convert curl commands to PHP code”
Answer:
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);
Answer:
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);
Answer:
Unfortunately SO still doesn’t have CommonMark table markup. This is an autogenerated list of which curl commandline options might map onto which php CURLOPT_ constant:
-a
– –append – Append to target file when uploading
→CURLOPT_FTPAPPEND
,CURLOPT_APPEND
- –basic – Use HTTP Basic Authentication
→CURLAUTH_BASIC
- –cacert – CA certificate to verify peer against
→CURLE_SSL_CACERT
,CURLE_SSL_CACERT_BADFILE
- –capath – CA directory to verify peer against
→CURLOPT_CAPATH
-E
– –cert – 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 – Connect to host
→CURLOPT_CONNECT_TO
-b
– –cookie – 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 – Get a CRL list in PEM format from the given file
→CURLOPT_CRLFILE
-d
– –data – HTTP POST data
→CURLOPT_READDATA
,CURL_LOCK_DATA_COOKIE
,CURL_LOCK_DATA_DNS
,CURL_LOCK_DATA_SSL_SESSION
- –delegation – 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 to use for DNS requests
→CURLOPT_DNS_INTERFACE
- –dns-servers – DNS server addrs to use
→CURLOPT_DNS_SERVERS
- –engine – 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 – 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 – Account data string
→CURLOPT_FTP_ACCOUNT
- –ftp-alternative-to-user – String to replace USER [name] →
CURLOPT_FTP_ALTERNATIVE_TO_USER
-P
– –ftp-port – 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 – 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 – Use network INTERFACE (or address)
→CURLOPT_INTERFACE
,CURLOPT_DNS_INTERFACE
-6
– –ipv6 – Resolve names to IPv6 addresses
→CURL_VERSION_IPV6
- –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 – Enable Kerberos with security
→CURLOPT_KRB4LEVEL
,CURLOPT_KRBLEVEL
- –local-port – Force use of RANGE for local port numbers
→CURLINFO_LOCAL_PORT
-L
– –location – Follow redirects
→CURLOPT_FOLLOWLOCATION
- –login-options – Server login options
→CURLOPT_LOGIN_OPTIONS
- –mail-auth – Originator address of the original email
→CURLOPT_MAIL_AUTH
- –mail-from – Mail from this address
→CURLOPT_MAIL_FROM
- –mail-rcpt – 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 – Specify FILE for netrc
→CURLOPT_NETRC_FILE
- –netrc-optional – Use either .netrc or URL
→CURL_NETRC_OPTIONAL
- –noproxy – 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 – OAuth 2 Bearer Token
→CURLOPT_XOAUTH2_BEARER
- –pass – 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 – FILE/HASHES Public key to verify peer against
→CURLE_SSL_PINNEDPUBKEYNOTMATCH
- –proto – 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
-x
– –proxy – [protocol://]host[:port] Use this proxy
→CURLOPT_HTTPPROXYTUNNEL
,CURLOPT_PROXY
,CURLOPT_PROXYPORT
,CURLOPT_PROXYTYPE
,CURLOPT_PROXYUSERPWD
,CURLE_COULDNT_RESOLVE_PROXY
,CURLPROXY_HTTP
,CURLPROXY_SOCKS4
,CURLPROXY_SOCKS5
,CURLOPT_PROXYAUTH
,CURLINFO_PROXYAUTH_AVAIL
,CURLOPT_PROXY_TRANSFER_MODE
,CURLPROXY_SOCKS4A
,CURLPROXY_SOCKS5_HOSTNAME
,CURLOPT_PROXYPASSWORD
,CURLOPT_PROXYUSERNAME
,CURLOPT_NOPROXY
,CURLPROXY_HTTP_1_0
,CURLOPT_PROXYHEADER
,CURLOPT_PROXY_SERVICE_NAME
- –proxy-service-name – SPNEGO proxy service name
→CURLOPT_PROXY_SERVICE_NAME
-p
– –proxytunnel – Operate through an HTTP proxy tunnel (using CONNECT)
→CURLOPT_HTTPPROXYTUNNEL
- –pubkey – 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 for reading random data from
→CURLOPT_RANDOM_FILE
-r
– –range – Retrieve only the bytes within RANGE
→CURLOPT_RANGE
,CURLE_HTTP_RANGE_ERROR
,CURLOPT_LOCALPORTRANGE
-e
– –referer – Referrer URL
→CURLOPT_AUTOREFERER
,CURLOPT_REFERER
-X
– –request – Specify request command to use
→CURLOPT_CUSTOMREQUEST
,CURLINFO_REQUEST_SIZE
,CURLOPT_RTSP_REQUEST
- –resolve – 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 – Retry request if transient problems occur
→CURLFTP_CREATE_DIR_RETRY
- –sasl-ir – Enable initial response in SASL authentication
→CURLOPT_SASL_IR
- –service-name – SPNEGO service name
→CURLOPT_PROXY_SERVICE_NAME
,CURLOPT_SERVICE_NAME
- –socks4 – SOCKS4 proxy on given host + port
→CURLPROXY_SOCKS4
,CURLPROXY_SOCKS4A
- –socks4a – SOCKS4a proxy on given host + port
→CURLPROXY_SOCKS4A
- –socks5 – 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 – SOCKS5 proxy service name for GSS-API
→CURLOPT_SOCKS5_GSSAPI_SERVICE
- –socks5-hostname – SOCKS5 proxy, pass host name to proxy
→CURLPROXY_SOCKS5_HOSTNAME
-Y
– –speed-limit – Stop transfers slower than this
→CURLOPT_LOW_SPEED_LIMIT
-y
– –speed-time – 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 – Set telnet option
→CURLE_TELNET_OPTION_SYNTAX
,CURLE_UNKNOWN_TELNET_OPTION
- –tftp-blksize – 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 – Connect through this Unix domain socket
→CURLOPT_UNIX_SOCKET_PATH
- –url – URL to work with
→CURLOPT_URL
-u
– –user – 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.
Answer:
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
Answer:
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.
Answer:
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);