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
You need ...
curl-to-PHP : https://incarnate.github.io/curl-to-php/
"Instantly convert curl commands to PHP code"
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);
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.
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);
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
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.
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);
Use curlconverter.com/php/, it will convert your curl command into
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://hostname/#api/deki/pages/=TestPage/files/=test.png');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type' => 'image/png',
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents('test.png'));
$response = curl_exec($ch);
curl_close($ch);
Related
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
You need ...
curl-to-PHP : https://incarnate.github.io/curl-to-php/
"Instantly convert curl commands to PHP code"
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);
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.
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);
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
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.
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);
Use curlconverter.com/php/, it will convert your curl command into
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://hostname/#api/deki/pages/=TestPage/files/=test.png');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type' => 'image/png',
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents('test.png'));
$response = curl_exec($ch);
curl_close($ch);
I have a curl command that worls perfectly form the SSH shell, but cannot get it working with PHP Version 7.4.3.
I have tried several times using just about every example I can find on the Internet, all of them return a blank html page when run.
Here is the curl command (with user/pass and IP etc changed for obvious reasons);
curl \
--user someuser:password \
--data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "getinfo", "params": [] }' \
-H 'content-type: text/plain;' \
http://195.86.111.23:8223/
Can someone please educate me on how to turn this into something PHP can use?
I am currently testing with this code, but get an error "{"result":null,"error":{"code":-32600,"message":"Params must be an array"},"id":"curltest"} "
$payload=array();
$payload['jsonrpc']='1.0';
$payload['id']='curltest';
$payload['method']='getinfo';
$payload['params']='[]';
$payload=json_encode($payload);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERPWD, $user . ":" . $pass);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_POSTREDIR, 3);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$res=curl_exec($ch);
echo $res;
I have also tried using an automated tool here - https://incarnate.github.io/curl-to-php/ which seems to convert my curl command to PHP okay, but the page is empty on loading the provided code.
$payload = ["jsonrpc" => "1.0", id => "curltest", "method" => "getinfo", "params" => [] ];
Simple solution!
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
You need ...
curl-to-PHP : https://incarnate.github.io/curl-to-php/
"Instantly convert curl commands to PHP code"
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);
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.
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);
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
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.
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);
Use curlconverter.com/php/, it will convert your curl command into
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://hostname/#api/deki/pages/=TestPage/files/=test.png');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type' => 'image/png',
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents('test.png'));
$response = curl_exec($ch);
curl_close($ch);
I'm working with a specific restful API on CakePHP 2.4.x, part of the API includes sending files via curl POST/PUT and we've been unable to replicate what we need in CakePHP's HttpSocket so opted for traditional curl. The attempts to use PHP's curl commands would output nothing at all (despite pauses to show they were running).
Setting up a shell_exec will output information but unfortunately we are not getting the response body which is important to save data or view specific error messages.
I'm adding everything I think is relevant to this issue (I am on OSX, local environment XAMPP). My cURL version prints:
curl 7.30.0 (x86_64-apple-darwin13.0) libcurl/7.30.0 SecureTransport zlib/1.2.5
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp smtp smtps telnet tftp
Features: AsynchDNS GSS-Negotiate IPv6 Largefile NTLM NTLM_WB SSL libz
Deprecated PHP Code (This prints string(0) "" )
$ch = curl_init();
curl_setopt ($ch, CURLOPT_HTTPHEADER, Array(
"Authorization: Bearer {$authInfo->access_token}",
'Accept: application/json'
));
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
$post[$data['file']['name']] = '#' . $data['file']['location'];
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_URL, $url);
$response = curl_exec($ch);
pr($post); // print_r wrapper
pr(curl_getinfo($ch));
var_dump($response);
curl_close($ch);
Shell Exec Code
This code works fine in actually doing what we want with the API, the only issue is we're not getting the response body.
$command = Configure::read('DM.CurlBinary') . ' -sb -k -v -X POST ';
$command .= $url . ' -F "' . $data['file']['name'] . '=#' . $data['file']['location'] . '"';
$command .= ' -H "AUTHORIZATION: Bearer ' . $authInfo->access_token . '"';
$command .= ' -H "ACCEPT: application/json" -H "EXPECT: 100-continue"';
$output = shell_exec($command);
pr($output); // print_r wrapper
pr($command);
Actual CURL Command
curl -sb -k -v -X POST https://idolcamp.idol.io/api/v1/tracks/857707/records -F "record[new_file]=#/Users/someuser/Documents/Development/DittoCake/app/tmp/Idol_857707.flac" -H "AUTHORIZATION: Bearer speacialtokengoeshere" -H "ACCEPT: application/json" -H "EXPECT: 100-continue"
Response
This shows as an 422 error because the file I am uploading already exists, I'd check it by its id but we have no response in order to get the id and check if it exists next time around.
Adding handle: conn: 0x7f8e3c004000
Adding handle: send: 0
Adding handle: recv: 0
Curl_addHandleToPipeline: length: 1
- Conn 0 (0x7f8e3c004000) send_pipe: 1, recv_pipe: 0
About to connect() to idolcamp.idol.io port 443 (#0)
Trying 212.83.129.122...
Connected to idolcamp.idol.io (212.83.129.122) port 443 (#0)
TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384
Server certificate: *.idol.io
Server certificate: Gandi Standard SSL CA
Server certificate: UTN-USERFirst-Hardware
POST /api/v1/tracks/857707/records HTTP/1.1
User-Agent: curl/7.30.0
Host: idolcamp.idol.io
AUTHORIZATION: Bearer somespecialtoken
ACCEPT: application/json
EXPECT: 100-continue
Content-Length: 29600116
Content-Type: multipart/form-data; boundary=----------------------------3bd64d03cd9b
HTTP/1.1 100 Continue
} [data not shown]
HTTP/1.1 422 Unprocessable Entity
Server nginx is not blacklisted
Server: nginx
Date: Tue, 25 Aug 2015 15:08:00 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 0
Connection: keep-alive
X-Request-Id: 3803269a-18ea-4558-8623-08810a058030
X-Runtime: 1.112917
HTTP error before end of send, stop sending
Closing connection 0
API Doc: Request
API Doc: Response
I have problem with interaction with Unistream service by SOAP-client and by curl too.
My environment: Ubuntu 15.04, php 5.64
Some history: unistream's support asked us generate certifiate:
makecert.exe -n "CN=some_name" -ss My -r -m 120 -pe -sky exchange -a sha1, then export by mmc end send certificate to them. Ok, we do this and got certificate generated on their side.
I convert their certificate to pem-format and try use is php SoapClient:
$soap = new SoapClient("http://test2.unistream.com:82/wcflib-tc/service.svc?wsdl", [
'encoding' => 'UTF-8',
'trace' => true,
'local_cert' => $cert_path,
'soap_version' => SOAP_1_2,
'connection_timeout' => 180,
'cache_wsdl' => WSDL_CACHE_NONE
]);
$data = $soap->GetCountriesChanges(['requestMessage'=>[
'AuthenticationHeader'=>[
'AppKey'=>'*',
'Password'=>'*',
'Username'=>'*',
],
'UpdateCount'=>1000
]]);
And i got nothing. I try the same with python soap library, with soapui and got nothing too, timeout.
Ok, i look it with wireshark, and saw that soap client recieve all xsd schemas and etc and then connection interrupt after post request to url: http://test2.unistream.com:82/wcflib-tc/service.svc, just timeout. And i saw only plain http with xml, no packets with ssl.
Ok, i try send post-request on this url:
ini_set('default_socket_timeout', 30);
$request_string =
'<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"
xmlns:ns1="http://schemas.datacontract.org/2004/07/WcfServiceLib"
xmlns:ns2="http://schemas.datacontract.org/2004/07/WcfServiceLib.Utils"
xmlns:ns3="http://schemas.datacontract.org/2004/07/WcfServiceLib.Dictionaries.Country"
xmlns:ns4="http://test.unistream.com/wcflib/">
<env:Body>
<ns4:GetCountriesChanges>
<ns4:requestMessage>
<ns2:AuthenticationHeader>
<ns1:AppKey>*</ns1:AppKey>
<ns1:Password>*</ns1:Password>
<ns1:Username>*</ns1:Username>
</ns2:AuthenticationHeader>
<ns3:UpdateCount>1000</ns3:UpdateCount>
</ns4:requestMessage>
</ns4:GetCountriesChanges>
</env:Body>
</env:Envelope>
';
$url = 'http://test2.unistream.com:82/wcflib-tc/service.svc';
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/soap+xml;', 'Charset=utf-8']);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_CAPATH, "/path");
//my cert
curl_setopt($ch, CURLOPT_SSLCERT, "/path/local_cert.pem");
//my private key
curl_setopt($ch, CURLOPT_SSLKEY, "/path/local_key.pem");
curl_setopt($ch, CURLOPT_SSLKEYPASSWD, 'passwd');
//api's cert
curl_setopt($ch, CURLOPT_CAINFO, '/path/api_cert.cer');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request_string);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_CERTINFO, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
print_r($result);
echo "\n";
$error = curl_error($ch);
print_r($error);
echo "\n";
And i get xml with error: "An error occurred when verifying security for the message."
If i try send request to url: https://test2.unistream.com:82/wcflib-tc/service.svc i got plain html with error 400 and curl error: "error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol"
Support can't help me. They said "we have 10000 working clients, we consult only about high-level api problems and bussines-logic".
Can somebody give me right direction?
I don't have docs for unistream.
I do know that certificates are transport layer. That means that they occur before anything else. But you're connecting to an HTTP URL, not an HTTPS URL and that means you're not using encryption.
Certificates and encryption are not your problem.
However, the URL you pasted is simply not listening for connections on that port.
$ curl -v http://test2.unistream.com:82/wcflib-tc/service.svc?wsdl
* Trying 94.127.155.67...
* Connection timed out after 60001 milliseconds
* Closing connection 0 curl: (28) Connection timed out after 60001 milliseconds
You're not using the right address/port number at all.
Next
Get the right url endpoint
don't bother with the client certificate unless the endpoint is using HTTPS