PHP-Ajax implements asynchronous uploading of pictures to Sina Picture Bed

藏色散人
Release: 2023-04-07 22:10:01
forward
2265 people have browsed it

Upload pictures to Sina Pictures asynchronously and obtain the external link address of the picture.

1. Fill in the pits

1. The Jquery library must be referenced in the page, otherwise asynchronous requests cannot be implemented. Baidu’s Jquery library is used here.

Copy after login

2. Related information links

Sina Tubed Upload Interface Source Code

Some people get the source code of this interface and don’t know how to use it. At first I don’t quite understand it, but I figured it out slowly. Thanks to the interface author for sharing.

Related recommendations: "PHP Tutorial"

2. Implementation code

1. Picture bed interface source code, only need Just modify the account password in the source code, no other modifications are required.

File name: imgfile.php

20*3600||$config['cookie']=='SUB;') {
  $cookie = login($sinauser,$sinapwd);
  if($cookie&&$cookie!='SUB;'){
    CookieSet($cookie,$time = time());
  }else{
    return error('203','获取cookie出现错误,请检查账号状态或者重新获取cookie');
  }
}
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {exit;}
$type=$_GET['type'];
if($type=='multipart'){
  $multipart = true;
  $file = $_FILES["file"]["tmp_name"];
}elseif(isset($_GET['img'])){
  $multipart = false;
  $file = $_GET['img'];
}else{
  return error('404','请勿直接访问');
}
if (isset($file) && $file != "") {
  include 'sina_config.php';
  $cookie = $config['cookie'];
  echo upload($file, $multipart,$cookie);
}else{
  return error('201','上传错误'.$file."结尾");
}
function CookieSet($cookie,$time){
  $newConfig = ' "'.$cookie.'",
    "time" => "'.$time.'",
  );';
  @file_put_contents('sina_config.php', $newConfig);
}
function error($code,$msg){
  $arr = array('code'=>$code,'msg'=>$msg);
  echo json_encode($arr);
}
/**
     * 新浪微博登录(无加密接口版本)
     * @param  string $u 用户名
     * @param  string $p 密码
     * @return string    返回最有用最精简的cookie
     */
function login($u,$p){
  $loginUrl = 'https://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.15)&_=1403138799543';
  $loginData['entry'] = 'sso';
  $loginData['gateway'] = '1';
  $loginData['from'] = 'null';
  $loginData['savestate'] = '30';
  $loginData['useticket'] = '0';
  $loginData['pagerefer'] = '';
  $loginData['vsnf'] = '1';
  $loginData['su'] = base64_encode($u);
  $loginData['service'] = 'sso';
  $loginData['sp'] = $p;
  $loginData['sr'] = '1920*1080';
  $loginData['encoding'] = 'UTF-8';
  $loginData['cdult'] = '3';
  $loginData['domain'] = 'sina.com.cn';
  $loginData['prelt'] = '0';
  $loginData['returntype'] = 'TEXT';
  return loginPost($loginUrl,$loginData); 
}
/**
     * 发送微博登录请求
     * @param  string $url  接口地址
     * @param  array  $data 数据
     * @return json         算了,还是返回cookie吧//返回登录成功后的用户信息json
     */
function loginPost($url,$data){
  $tmp = '';
  if(is_array($data)){
    foreach($data as $key =>$value){
      $tmp .= $key."=".$value."&";
    }
    $post = trim($tmp,"&");
  }else{
    $post = $data;
  }
  $ch = curl_init();
  curl_setopt($ch,CURLOPT_URL,$url); 
  curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 
  curl_setopt($ch,CURLOPT_HEADER,1);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  curl_setopt($ch,CURLOPT_POST,1);
  curl_setopt($ch,CURLOPT_POSTFIELDS,$post);
  $return = curl_exec($ch);
  curl_close($ch);
  return 'SUB' . getSubstr($return,"Set-Cookie: SUB",'; ') . ';';
}
/**
 * 取本文中间
 */
function getSubstr($str,$leftStr,$rightStr){
  $left = strpos($str, $leftStr);
  //echo '左边:'.$left;
  $right = strpos($str, $rightStr,$left);
  //echo '
右边:'.$right; if($left <= 0 or $right < $left) return ''; return substr($str, $left + strlen($leftStr), $right-$left-strlen($leftStr)); } function upload($file, $multipart = true,$cookie) { $url = 'http://picupload.service.weibo.com/interface/pic_upload.php'.'?mime=image%2Fjpeg&data=base64&url=0&markpos=1&logo=&nick=0&marks=1&app=miniblog'; if($multipart) { $url .= '&cb=http://weibo.com/aj/static/upimgback.html?_wv=5&callback=STK_ijax_'.time(); if (class_exists('CURLFile')) { // php 5.5 $post['pic1'] = new \CURLFile(realpath($file)); } else { $post['pic1'] = '@'.realpath($file); } } else { $post['b64_data'] = base64_encode(file_get_contents($file)); } // Curl提交 $ch = curl_init($url); curl_setopt_array($ch, array( CURLOPT_POST => true, CURLOPT_VERBOSE => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => array("Cookie: $cookie"), CURLOPT_POSTFIELDS => $post, )); $output = curl_exec($ch); curl_close($ch); // 正则表达式提取返回结果中的json数据 preg_match('/({.*)/i', $output, $match); if(!isset($match[1])) return error('201','上传错误'); $a=json_decode($match[1],true); $width = $a['data']['pics']['pic_1']['width']; $size = $a['data']['pics']['pic_1']['size']; $height = $a['data']['pics']['pic_1']['height']; $pid = $a['data']['pics']['pic_1']['pid']; if(!$pid){return error('201','上传错误');} $arr = array('code'=>'200','width'=>$width,"height"=>$height,"size"=>$size,"pid"=>$pid,"url"=>"http://ws3.sinaimg.cn/thumb150/".$pid.".jpg"); return json_encode($arr); } ?>
Copy after login

2. Simple image file upload page (no php environment required)

File name: upfile.html




    
    
    
    
    

显示结果


Copy after login

3. Picture

1. Select a picture

PHP-Ajax implements asynchronous uploading of pictures to Sina Picture Bed

2.Click the

displayed after uploading PHP-Ajax implements asynchronous uploading of pictures to Sina Picture Bed

The above is the detailed content of PHP-Ajax implements asynchronous uploading of pictures to Sina Picture Bed. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!