将命令行cURL转换为PHP cURL

0tdrvxhp  于 2022-12-13  发布在  PHP
关注(0)|答案(9)|浏览(209)

我从来没有做过任何 curl 之前,所以我需要一些帮助。我试图工作,这从例子,但不能得到我的头周围!
我有一个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
谢谢你的帮助。

zf2sa74q

zf2sa74q1#

起点:

<?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

...
?>

另请参阅:http://www.php.net/manual/en/function.curl-setopt.php

nx7onnlm

nx7onnlm2#

你需要...

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

  • “立即将curl命令转换为PHP代码”*
h9vpoimq

h9vpoimq3#

无论命令行中有什么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);
ttisahbt

ttisahbt4#

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.

wj8zmpe1

wj8zmpe15#

试试这个:

$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);
fslejnso

fslejnso6#

libcurl选项就是为此目的而添加的,尽管它生成的是C程序,但我认为它应该很容易翻译成PHP

oaxa6hgo

oaxa6hgo7#

以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_setopt。

798qvoo8

798qvoo88#

最好这样。排成一行。

$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);
d4so4syb

d4so4syb9#

使用curlconverter.com/php/,它会将curl命令转换为

<?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);

相关问题