我从来没有做过任何卷曲,所以需要一些帮助。我试图从例子中解决这个问题,但无法理解它!
我有一个curl命令,我可以从linux(ubuntu)命令行成功运行,该命令行通过api将文件放到wiki中。
我需要将这个curl命令合并到我正在构建的PHP脚本中。
如何翻译此curl命令以使其在PHP脚本中运行?
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包含身份验证,但我在脚本中以明文形式显示这个问题没有问题,因为这只会由我运行。
@ test.png必须是一个变量,例如$ filename
http://hostname/@api/deki/pages/=TestPage/files/=必须是变量,例如$ pageurl
感谢您的帮助。
答案 0 :(得分:28)
一个起点:
<?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
...
?>
答案 1 :(得分:11)
答案 2 :(得分:6)
您在命令行中拥有cURL,可以使用此工具将其转换为PHP:
https://incarnate.github.io/curl-to-php/
经过长时间寻找解决方案后,它帮助了我!希望它也会帮助你!你的解决方案是:
// 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);
答案 3 :(得分:5)
试试这个:
$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);
答案 4 :(得分:2)
为此目的添加了--libcurl选项,即使它创建了一个C程序,我认为它应该很容易转换为PHP
答案 5 :(得分:2)
使用MYYN的答案作为起点,this page作为如何使用PHP cURL发送POST数据的参考,这是我的建议(我正在研究一些非常相似的东西):
<?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);
?>
如果需要,您可以使用curl_setopt_array()调用来优化许多curl_setopts。
答案 6 :(得分:2)
不幸的是,SO仍然没有CommonMark表标记。这是一个自动生成的列表,其中curl commandline options可能会映射到哪个php CURLOPT_常量:
-a
– -追加 –上传时追加到目标文件CURLOPT_FTPAPPEND
, CURLOPT_APPEND
CURLAUTH_BASIC
CURLE_SSL_CACERT
, CURLE_SSL_CACERT_BADFILE
CURLOPT_CAPATH
-E
– -cert –客户端证书文件和密码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
CURLOPT_CONNECT_TO
-b
– -cookie –从字符串/文件发送cookie CURLOPT_COOKIE
, CURLOPT_COOKIEFILE
, CURLOPT_COOKIEJAR
, CURLOPT_COOKIESESSION
, CURL_LOCK_DATA_COOKIE
, CURLINFO_COOKIELIST
, CURLOPT_COOKIELIST
CURLOPT_CRLF
, CURLOPT_CRLFILE
CURLOPT_CRLFILE
-d
– -data – HTTP POST数据CURLOPT_READDATA
, CURL_LOCK_DATA_COOKIE
, CURL_LOCK_DATA_DNS
, CURL_LOCK_DATA_SSL_SESSION
CURLGSSAPI_DELEGATION_FLAG
, CURLGSSAPI_DELEGATION_POLICY_FLAG
, CURLOPT_GSSAPI_DELEGATION
CURLAUTH_DIGEST
, CURLAUTH_DIGEST_IE
CURLOPT_DNS_INTERFACE
CURLOPT_DNS_SERVERS
CURLOPT_SSLENGINE
, CURLOPT_SSLENGINE_DEFAULT
, CURLE_SSL_ENGINE_NOTFOUND
, CURLE_SSL_ENGINE_SETFAILED
, CURLINFO_SSL_ENGINES
-f
– -失败 –因HTTP错误而静默失败(根本没有输出)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 –指定多部分MIME数据CURLE_FTP_WEIRD_227_FORMAT
, CURLE_MALFORMAT_USER
, CURLE_URL_MALFORMAT
, CURLE_URL_MALFORMAT_USER
, CURLM_CALL_MULTI_PERFORM
CURLOPT_FTP_ACCOUNT
CURLOPT_FTP_ALTERNATIVE_TO_USER
-P
– -ftp-port –使用PORT代替PASV CURLE_FTP_PORT_FAILED
CURLOPT_FTP_SKIP_PASV_IP
CURLOPT_FTP_SSL_CCC
-G
– -获取 –将帖子数据放入URL并使用GET CURLOPT_HTTPGET
, CURLE_FTP_CANT_GET_HOST
, CURLE_FTP_COULDNT_GET_SIZE
, CURL_RTSPREQ_GET_PARAMETER
-I
– -head –仅显示文档信息CURLOPT_HEADER
, CURLOPT_HEADERFUNCTION
, CURLOPT_HTTPHEADER
, CURLOPT_WRITEHEADER
, CURLINFO_HEADER_OUT
, CURLINFO_HEADER_SIZE
, CURLHEADER_SEPARATE
, CURLHEADER_UNIFIED
, CURLOPT_HEADEROPT
, CURLOPT_PROXYHEADER
-H
– -标头 –将自定义标头传递到服务器CURLOPT_HEADER
, CURLOPT_HEADERFUNCTION
, CURLOPT_HTTPHEADER
, CURLOPT_WRITEHEADER
, CURLINFO_HEADER_OUT
, CURLINFO_HEADER_SIZE
, CURLHEADER_SEPARATE
, CURLHEADER_UNIFIED
, CURLOPT_HEADEROPT
, CURLOPT_PROXYHEADER
CURLOPT_HTTP200ALIASES
, CURL_VERSION_HTTP2
CURLOPT_IGNORE_CONTENT_LENGTH
CURLOPT_INTERFACE
, CURLOPT_DNS_INTERFACE
-6
– -ipv6 –将名称解析为IPv6地址CURL_VERSION_IPV6
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
CURLOPT_KRB4LEVEL
, CURLOPT_KRBLEVEL
CURLINFO_LOCAL_PORT
-L
– -位置 –跟随重定向CURLOPT_FOLLOWLOCATION
CURLOPT_LOGIN_OPTIONS
CURLOPT_MAIL_AUTH
CURLOPT_MAIL_FROM
CURLOPT_MAIL_RCPT
CURLAUTH_GSSNEGOTIATE
, CURLAUTH_NEGOTIATE
-n
– -netrc –必须读取.netrc作为用户名和密码CURLOPT_NETRC
, CURL_NETRC_IGNORED
, CURL_NETRC_OPTIONAL
, CURL_NETRC_REQUIRED
, CURLOPT_NETRC_FILE
CURLOPT_NETRC_FILE
CURL_NETRC_OPTIONAL
CURLOPT_NOPROXY
CURLAUTH_NTLM
, CURLAUTH_NTLM_WB
CURLAUTH_NTLM_WB
CURLOPT_XOAUTH2_BEARER
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
CURLOPT_PATH_AS_IS
CURLE_SSL_PINNEDPUBKEYNOTMATCH
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]使用此代理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
CURLOPT_PROXY_SERVICE_NAME
-p
– -proxytunnel –通过HTTP代理隧道(使用CONNECT)进行操作CURLOPT_HTTPPROXYTUNNEL
CURLE_SSL_PINNEDPUBKEYNOTMATCH
-Q
– -报价 –传输前将命令发送到服务器CURLOPT_POSTQUOTE
, CURLOPT_PREQUOTE
, CURLOPT_QUOTE
, CURLE_FTP_QUOTE_ERROR
CURLOPT_RANDOM_FILE
-r
– -range –仅检索RANGE中的字节CURLOPT_RANGE
, CURLE_HTTP_RANGE_ERROR
, CURLOPT_LOCALPORTRANGE
-e
– -引荐来源网址 –引荐来源网址CURLOPT_AUTOREFERER
, CURLOPT_REFERER
-X
– -请求 –指定要使用的请求命令CURLOPT_CUSTOMREQUEST
, CURLINFO_REQUEST_SIZE
, CURLOPT_RTSP_REQUEST
CURLE_COULDNT_RESOLVE_HOST
, CURLE_COULDNT_RESOLVE_PROXY
, CURLOPT_IPRESOLVE
, CURL_IPRESOLVE_V4
, CURL_IPRESOLVE_V6
, CURL_IPRESOLVE_WHATEVER
, CURLOPT_RESOLVE
CURLFTP_CREATE_DIR_RETRY
CURLOPT_SASL_IR
CURLOPT_PROXY_SERVICE_NAME
, CURLOPT_SERVICE_NAME
CURLPROXY_SOCKS4
, CURLPROXY_SOCKS4A
CURLPROXY_SOCKS4A
CURLPROXY_SOCKS5
, CURLPROXY_SOCKS5_HOSTNAME
, CURLOPT_SOCKS5_GSSAPI_NEC
, CURLOPT_SOCKS5_GSSAPI_SERVICE
CURLOPT_SOCKS5_GSSAPI_NEC
, CURLOPT_SOCKS5_GSSAPI_SERVICE
CURLOPT_SOCKS5_GSSAPI_NEC
CURLOPT_SOCKS5_GSSAPI_SERVICE
CURLPROXY_SOCKS5_HOSTNAME
-Y
– -限速 –停止传输的速度低于此速度CURLOPT_LOW_SPEED_LIMIT
-y
– -速度时间 –在此时间之后触发“限速”中止CURLOPT_LOW_SPEED_TIME
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 –使用SSLv2 CURL_SSLVERSION_SSLv2
-3
– -sslv3 –使用SSLv3 CURL_SSLVERSION_SSLv3
CURLOPT_STDERR
CURLOPT_TCP_FASTOPEN
CURLOPT_TCP_NODELAY
-t
– -telnet-option –设置telnet选项CURLE_TELNET_OPTION_SYNTAX
, CURLE_UNKNOWN_TELNET_OPTION
CURLOPT_TFTP_BLKSIZE
CURLOPT_TFTP_NO_OPTIONS
-1
– -tlsv1 –使用TLSv1.0或更高版本CURL_SSLVERSION_TLSv1
, CURL_SSLVERSION_TLSv1_0
, CURL_SSLVERSION_TLSv1_1
, CURL_SSLVERSION_TLSv1_2
CURLOPT_UNIX_SOCKET_PATH
CURLOPT_URL
-u
– -用户 –服务器用户和密码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 –使操作更加健谈CURLOPT_VERBOSE
-V
– -版本 –显示版本号并退出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
请注意,这仅列出--long选项与类似名称的CURLOPT_常量的某些精确匹配。但这应该给您足够的提示,以了解如何比较curl --help
输出和PHP curl_setopt()列表。
答案 7 :(得分:1)
更好的这一点。在一行。
$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);