For several reasons I don't want to use curl
, and I have known that WordPress has methods for HTTP request, so I choose wp_remote_post()
, which allows you to send an HTTP post request, and return an array. Here's the code example:
<?php
$upload_url = "URL_YOUR_UPLOAD_FILE_TO";
$local_file = "PATH/TO/FILE";
$filename = basename($local_file);
$name = $local_file; //name of Form Control
$Boundary = wp_generate_password(); //split signal, see: https://www.ietf.org/rfc/rfc1867.txt
$bits = file_get_contents($local_file);
$args = array(
"headers" => "Content-Type: multipart/form-data; boundary=$Boundary\r\n\r\nAuthorization: Basic $client_id\r\n\r\nUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97",
"body" => "--$Boundary\r\nContent-Disposition: form-data; name=\"$name\"; filename=\"$filename\"\r\n\r\n$bits\r\n\r\n--$Boundary--"
);
$response = wp_remote_post($upload_url, $args);
if (!is_wp_error($response)) {
$reply = $response["body"]; //the remote server's reply
var_dump($reply);
}
?>
reference: https://www.jianshu.com/p/29e38bcc8a1d
恭喜,你成功屏蔽了广告 *这是一则由 Google AdSense 自动推荐的广告,不代表本站立场
Comments | 4 条评论
wp_remote_post其实也是用curl实现的… https://core.trac.wordpress.org/browser/tags/5.3/src/wp-includes/Requests/Transport/cURL.php#L162
@Mashiro 嗯,选
wp_remote_post
主要是因为标准化使用好看些,我再简化下,把 array_push 去了该评论为私密评论
@Ekkles 了解
