WP博客内容同步新浪微博,非OAuth 2.0实现代码

发布于 / 教程 / 1 条评论

wp博客内容同步新浪微博,非OAuth 2.0实现代码。

此文已同步至新浪微博。
WP博客内容同步新浪微博,非OAuth 2.0实现代码



function post_to_sina_weibo($POST_ID, $debug = true) {
    $api_url = 'https://api.weibo.com/2/statuses/share.json';
    $app_key = '12345678'; //修改为自己的app key
    $user_pwd = 'HHGGHYHFGJDKHFGJDJFDHJ=='; //base64(user:password)
    if (wp_is_post_revision($POST_ID)) return; //修订版本不发布。
    $post_status = get_post($POST_ID)->post_status;
    $post_title = get_post($POST_ID)->post_title;
    $post_content = get_post($POST_ID)->post_content;
    if ($post_status == 'publish' && $_POST['original_post_status'] != 'publish') {
        $status = '【' . strip_tags($post_title) . '】 ' . get_permalink($POST_ID) . mb_strimwidth(strip_tags(apply_filters('the_content', $post_content)) , 0, 150, '...');
        $status = mb_strimwidth($status, 0, 128*2+mb_strlen(get_permalink($POST_ID),'utf-8'),'...','utf-8');
		//链接算十个字,共140个字,...算2个字
        preg_match_all('/WP博客内容同步新浪微博,非OAuth 2.0实现代码/i', $post_content, $matches);
        $first_img = $matches[1][0]; //将文章第一张图片的地址赋值给$first_img
        if (empty($first_img)) { // 文章第一张图为空,
            $request = new WP_Http;
            $headers = array(
                'Authorization' => 'Basic ' . $user_pwd,
                'Content-Type' => 'application/x-www-form-urlencoded'
            );
            $body = array(
                'source' => $app_key,
                'status' => $status
            );
            $result = $request->post($api_url, array(
                'body' => $body,
                'headers' => $headers
            ));
            if ($debug) {
                logInfo($result['body']);
            }
        } else {
            $array = explode('?', basename($first_img));
            $filename = $array[0];
            $filecontent = file_get_contents($first_img);
            $boundary = uniqid('------------------');
            $MPboundary = '--' . $boundary;
            $endMPboundary = $MPboundary . '--';
            $multipartbody = '';
            $multipartbody.= $MPboundary . "\r\n";
            $multipartbody.= 'content-disposition: form-data; name="source' . "\"\r\n\r\n";
            $multipartbody.= urlencode($app_key) . "\r\n";
            $multipartbody.= $MPboundary . "\r\n";
            $multipartbody.= 'content-disposition: form-data; name="status' . "\"\r\n\r\n";
            $multipartbody.= urlencode($status) . "\r\n";
            $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.= $endMPboundary;
            $headers = array(
                'Authorization: Basic ' . $user_pwd,
                'content-Type: multipart/form-data; boundary=' . $boundary
            );
            $ci = curl_init();
            curl_setopt($ci, CURLOPT_URL, $api_url);
            curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
            curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE); //关闭SSL,否则校验证书失败
            curl_setopt($ci, CURLOPT_POST, TRUE);
            curl_setopt($ci, CURLOPT_POSTFIELDS, $multipartbody);
            $curlResponse = curl_exec($ci);
            $err = curl_error($ci);
            curl_close($ci);
            if ($debug) {
                logInfo($curlResponse);
                if ($err) {
                    logInfo("cURL Error #:" . $err);
                }
            }
        }
    }
}
// 写日志函数
function logInfo($msg) {
	$path = get_template_directory();
    $logFile = $path.'/sync_weibo.log'; // 日志路径
    date_default_timezone_set('Asia/Shanghai');
    file_put_contents($logFile, date('[Y-m-d H:i:s]: ') . $msg . PHP_EOL, FILE_APPEND);
    return $msg;
}
add_action('publish_post', 'post_to_sina_weibo', 0); //0代表优先级,默认10,最小。

定制开发

接受各种定制开发,微信咨询:

前往查看

赞助支持

转载原创文章请注明,转载自: LYLARES BLOG » WP博客内容同步新浪微博,非OAuth 2.0实现代码

  1. 学习下,试试