首页 胡乱折腾🚀

无意间发现百度的一个产品知秋云客服并抓了里面的上传接口 然后做了个图片外链的PHP接口

知秋云接口

接口地址:https://zhiqiu.baidu.com/imcswebchat/api/file/upload

PHP代码

<?php
error_reporting(0);
header('Access-Control-Allow-Origin:*');
header('Content-Type: application/json; charset=utf-8');
if(!$file = $_FILES['file']['tmp_name']){
    exit;
}
$img = time().'.jpg';
copy($file,$img);
$ch = curl_init();
curl_setopt($ch,CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL,'https://zhiqiu.baidu.com/imcswebchat/api/file/upload');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,['file' => new \CURLFile(realpath($img))]);
$state = curl_exec($ch);
unlink($img);
if($state === false){
    echo 'Curl error: ' . curl_error($ch);
}else{
    $dejson = json_decode($state,true);
    $newjson = array();
    $newjson["state"] = $dejson["state"];
    $newjson["url"] = $dejson["url"];
    $endjson = json_encode($newjson);
    echo $endjson;
}
curl_close($ch);

本站接口

接口地址:https://blog.yxbug.cn/upimglink.php

结语

明天再写下前端部分代码 现在这个站点的图片外链也是使用这个接口 希望不要那么快凉


代码优化 2020.05.13

理论上只要接口不黄上传一般不会失败 如果真的是黄了返回报错信息对用户和前端是不太友好的 所以就把上传失败部分改成输出json

<?
error_reporting(0);
header('Access-Control-Allow-Origin:*');
header('Content-Type: application/json; charset=utf-8');
if(!$file = $_FILES['file']['tmp_name']){
    exit;
}
$img = time().'.jpg';
copy($file,$img);
$ch = curl_init();
curl_setopt($ch,CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL,'https://zhiqiu.baidu.com/imcswebchat/api/file/upload');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,['file' => new \CURLFile(realpath($img))]);
$state = curl_exec($ch);
unlink($img);
curl_close($ch);
$newjson = array();
if($state === false){
    $newjson["state"] = "FAILURE";
    $newjson["error"] = curl_error($ch);
}else{
    $dejson = json_decode($state,true);
    $newjson["state"] = $dejson["state"];
    $newjson["url"] = $dejson["url"];
}
$endjson = json_encode($newjson);
echo $endjson;