<commit_before><?php namespace Gibson\Wechat;

use Overtrue\Wechat\Exception;
use Overtrue\Wechat\Utils\Http;
use Overtrue\Wechat\Utils\JSON;

class HttpClient extends Http
{

    /**
     * @var string
     */
    protected $authorizer_access_token;

    /**
     * @var string
     */
    protected $component_access_token;

    /**
     * json请求
     *
     * @var bool
     */
    protected $json = false;

    /**
     * 发起一个HTTP/HTTPS的请求
     *
     * @param string $url 接口的URL
     * @param string $method 请求类型   GET | POST
     * @param array $params 接口参数
     * @param array $options 其它选项
     *
     * @return array | boolean
     */
    public function request($url, $method = self::GET, $params = array(), $options = array())
    {
        if ($this->authorizer_access_token) {
            $url .= (stripos($url, '?') ? '&' : '?') . 'access_token=' . $this->authorizer_access_token;
        }

        if ($this->component_access_token) {
            $url .= (stripos($url, '?') ? '&' : '?') . 'component_access_token=' . $this->component_access_token;
        }

        $method = strtoupper($method);

        if ($this->json) {
            $options['json'] = true;
        }

        $response = parent::request($url, $method, $params, $options);

        $this->json = false;

        if (empty($response['data'])) {
            throw new Exception('服务器无响应');
        }

        // 文本或者json
        $textMIME = '~application/json|text/plain~i';

        $contents = JSON::decode($response['data'], true);

        // while the response is an invalid JSON structure, returned the source data
        if (!preg_match($textMIME, $response['content_type'])
            || (JSON_ERROR_NONE !== json_last_error() && false === $contents)
        ) {
            return $response['data'];
        }

        if (isset($contents['errcode']) && 0 !== $contents['errcode']) {
            if (empty($contents['errmsg'])) {
                $contents['errmsg'] = 'Unknown';
            }

            throw new Exception("[{$contents['errcode']}] " . $contents['errcode'], $contents['errcode']);
        }

        if ($contents === array('errcode' => '0', 'errmsg' => 'ok')) {
            return true;
        }

        return $contents;
    }

    /**
     * 魔术调用
     *
     * @param string $method
     * @param array $args
     *<commit_msg># Exception 没有输出 errmsg<commit_after><?php namespace Gibson\Wechat;

use Overtrue\Wechat\Exception;
use Overtrue\Wechat\Utils\Http;
use Overtrue\Wechat\Utils\JSON;

class HttpClient extends Http
{

    /**
     * @var string
     */
    protected $authorizer_access_token;

    /**
     * @var string
     */
    protected $component_access_token;

    /**
     * json请求
     *
     * @var bool
     */
    protected $json = false;

    /**
     * 发起一个HTTP/HTTPS的请求
     *
     * @param string $url 接口的URL
     * @param string $method 请求类型   GET | POST
     * @param array $params 接口参数
     * @param array $options 其它选项
     *
     * @return array | boolean
     */
    public function request($url, $method = self::GET, $params = array(), $options = array())
    {
        if ($this->authorizer_access_token) {
            $url .= (stripos($url, '?') ? '&' : '?') . 'access_token=' . $this->authorizer_access_token;
        }

        if ($this->component_access_token) {
            $url .= (stripos($url, '?') ? '&' : '?') . 'component_access_token=' . $this->component_access_token;
        }

        $method = strtoupper($method);

        if ($this->json) {
            $options['json'] = true;
        }

        $response = parent::request($url, $method, $params, $options);

        $this->json = false;

        if (empty($response['data'])) {
            throw new Exception('服务器无响应');
        }

        // 文本或者json
        $textMIME = '~application/json|text/plain~i';

        $contents = JSON::decode($response['data'], true);

        // while the response is an invalid JSON structure, returned the source data
        if (!preg_match($textMIME, $response['content_type'])
            || (JSON_ERROR_NONE !== json_last_error() && false === $contents)
        ) {
            return $response['data'];
        }

        if (isset($contents['errcode']) && 0 !== $contents['errcode']) {
            if (empty($contents['errmsg'])) {
                $contents['errmsg'] = 'Unknown';
            }

            throw new Exception("[{$contents['errcode']}] " . $contents['errmsg'], $contents['errcode']);
        }

        if ($contents === array('errcode' => '0', 'errmsg' => 'ok')) {
            return true;
        }

        return $contents;
    }

    /**
     * 魔术调用
     *
     * @param string $method
     * @param array $args
     *
{'fraction_non_alphanumeric': 0.1654981941788825, 'fraction_numerical': 0.0008497981729339282, 'mean_word_length': 2.0611183355006504, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4916869', 'n_tokens_mistral': 1495, 'n_tokens_neox': 1467, 'n_words': 293}