Skip to content

Commit

Permalink
Merge pull request #9 from huo-zi/main
Browse files Browse the repository at this point in the history
替换curl为guzzlehttp;修复上传文件功能
  • Loading branch information
huozi1024 authored May 19, 2021
2 parents aad9a55 + d0fd04f commit e61bf31
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 41 deletions.
10 changes: 7 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
{
"name" : "huo-zi/work-wechat-robot",
"require-dev" : {
"monolog/monolog" : "^1.22 || ^2.0"
"require" : {
"monolog/monolog" : "^1.22 || ^2.0",
"guzzlehttp/guzzle": "^6.2 || ^7.0"
},
"require-dev": {
"phpunit/phpunit": "^8.0"
},
"description" : "企业微信机器人sdk",
"type" : "library",
Expand All @@ -21,4 +25,4 @@
"Huozi\\WorkWechat\\" : "src"
}
}
}
}
30 changes: 30 additions & 0 deletions src/WorkWechatClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Huozi\WorkWechat;

class WorkWechatClient
{

/**
* @var string
*/
protected $baseUri = 'https://qyapi.weixin.qq.com';

public function __construct()
{

}

public function request($uri, $method = 'GET', $options = [])
{
$response = $this->getClient()->request($method, $uri, $options);
return $response->getBody();
}

protected function getClient()
{
return new \GuzzleHttp\Client([
'base_uri' => $this->baseUri,
]);
}
}
61 changes: 23 additions & 38 deletions src/WorkWechatRobot.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
class WorkWechatRobot
{

private $robotUrl = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/';
/**
* @var WorkWechatClient
*/
private $client;

private $robotKey;

public function __construct($key)
{
$this->robotKey = $key;
$this->client = new WorkWechatClient();
}

public function setRobotKey($key)
Expand Down Expand Up @@ -81,56 +85,37 @@ public function image($imageFile)
]);
}

public function file($mediaId)
public function file($file)
{
$response = $this->upload($file);
if (!($result = json_decode($response)) && $result->errcode <> '0') {
return $response;
}
return $this->message([
'msgtype' => 'file',
'file' => [
'media_id' => $mediaId
'media_id' => $result->media_id
]
]);
}

private function message($message)
public function message($message)
{
return $this->send('send', json_encode($message), [
'Content-Type: application/json'
return $this->client->request('cgi-bin/webhook/send?key=' . $this->robotKey,'POST', [
'json' => $message
]);
}

private function upload($file)
public function upload($file)
{
return $this->send('upload_media', [
'media' => $file
], [
'Content-Type: multipart/form-data'
return $this->client->request('cgi-bin/webhook/upload_media?type=file&key=' . $this->robotKey,'POST', [
'multipart' => [
[
'name' => 'media',
'contents' => fopen($file, 'r'),
'file_name' => basename($file)
]
]
]);
}

private function send($type, $data, $headers)
{
$url = $this->robotUrl . $type . '?key=' . $this->robotKey . ($type == 'upload_media' ? '&type=file' : '');
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FAILONERROR, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($curl);
$res_code = intval(curl_getinfo($curl, CURLINFO_HTTP_CODE));
$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
if ($result === false || $res_code !== 200) {
$message = sprintf('curl error: %s, errno: %d, response_code: %d', curl_error($curl), curl_errno($curl), $res_code);
curl_close($curl);
return $message; // ;
}
curl_close($curl);
return substr($result, $header_size);
}
}

0 comments on commit e61bf31

Please sign in to comment.