Sdapi.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace app\service\api;
  3. use app\service\ConfServiceFacade;
  4. use laytp\traits\Error;
  5. use think\facade\Cache;
  6. use think\facade\Config;
  7. use think\facade\Request;
  8. class Sdapi
  9. {
  10. use Error;
  11. /**
  12. * phpmailer对象
  13. */
  14. protected $client;
  15. protected $key;
  16. protected $host;
  17. /**
  18. * 构造函数
  19. */
  20. public function __construct()
  21. {
  22. $conf = ConfServiceFacade::groupGet('system.ai');
  23. $this->key = $conf['ins_api_key'];
  24. $this->sk = $conf['ins_api_sk'];
  25. $this->host = 'https://stablediffusionapi.com/api/v3/';
  26. if(!$this->key){
  27. $this->setError('接口参数未正确配置');
  28. }
  29. }
  30. /**
  31. * POST 请求
  32. * @param string $url
  33. * @param array $param
  34. * @param boolean $post_file 是否文件上传
  35. * @return string content
  36. */
  37. private function http_post($url, $param, $jsonpost = false, $post_file = false) {
  38. $oCurl = curl_init();
  39. if (stripos($url, "https://") !== FALSE) {
  40. curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
  41. curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
  42. curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
  43. }
  44. $httpHeaders = array("Content-Type:application/json");
  45. curl_setopt($oCurl, CURLOPT_URL, $url);
  46. curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
  47. curl_setopt($oCurl, CURLOPT_POST, true);
  48. curl_setopt($oCurl, CURLOPT_HTTPHEADER, $httpHeaders);
  49. curl_setopt($oCurl, CURLOPT_POSTFIELDS, json_encode($param));
  50. $sContent = curl_exec($oCurl);
  51. $sContent = json_decode($sContent,true);
  52. $aStatus = curl_getinfo($oCurl);
  53. curl_close($oCurl);
  54. return $sContent;
  55. if ($sContent["status"] == 200) {
  56. return $sContent;
  57. } else {
  58. return false;
  59. }
  60. }
  61. }