[全新接口]WordPress 新文章带图片同步微博
add_action('publish_post', 'new_post_weibo', 1);
function new_post_weibo($post_ID, $debug = false) {
global $post;
if (!wp_is_post_revision($post_ID) && $post->post_status != "publish" || $debug == true){
if (isset($post) && $post->post_type != "post") return;
$access_token = "应用密匙";
$headers = array();
$headers[] = "Authorization: OAuth2 ".$access_token;
$url = 'https://api.weibo.com/2/statuses/share.json';
$status = "我刚刚发布了新文章《".get_the_title($post_ID)."》,快来看看吧。详细内容请点击:".get_permalink($post_ID);
if (has_post_thumbnail()) {
$post_thumbnail_id = get_post_thumbnail_id($post_ID);
$img_src = wp_get_attachment_url( $post_thumbnail_id );
}else{
$content = get_post( $post_ID )->post_content;
preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
$img_src = $strResult[1][0];
}
if(!empty($img_src)) {
$picfile = str_replace(home_url(),$_SERVER["DOCUMENT_ROOT"],$img_src);
if(!empty($picfile)){
$filecontent = file_get_contents($picfile);
}else{
$filecontent = file_get_contents($img_src);
}
$array = explode('?', basename($img_src));
$filename = $array[0];
$boundary = uniqid('------------------');
$MPboundary = '--'.$boundary;
$endMPboundary = $MPboundary. '--';
$multipartbody = '';
$multipartbody .= $MPboundary . "\r\n";
$multipartbody .= 'Content-Disposition: form-data; name="pic"; filename="' . $filename . '"' . "\r\n";
$multipartbody .= "Content-Type: image/unknown\r\n\r\n";
$multipartbody .= $filecontent. "\r\n";
$multipartbody .= $MPboundary . "\r\n";
$multipartbody .= 'content-disposition: form-data; name="status' . ""\r\n\r\n";
$multipartbody .= urlencode($status)."\r\n";
$multipartbody .= $endMPboundary;
$headers[] = "Content-Type: multipart/form-data; boundary=" . $boundary;
$data = $multipartbody;
}else{
$data = "status=" . urlencode($status);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$curlResponse = curl_exec($ch);
curl_close($ch);
$output = json_decode($curlResponse);
if($debug){
var_dump($output);
echo '<hr />';
var_dump($data);
}
}
}
作者:kn007的个人博客
自己改下代码里的“应用密匙”
最后更新于 2022-06-24 22:34:27 并被添加「」标签,已有 1217 位网友阅读过。
本站使用「署名 4.0 国际」创作共享协议,可自由转载、引用,但需署名作者且注明文章出处
此处评论已关闭