Gaoming13\WechatPhpSdk\Utils\HttpCurl::post PHP Method

post() public static method

模拟POST请求
public static post ( string $url, array $fields, string $data_type = 'text' ) : mixed
$url string
$fields array
$data_type string
return mixed Examples: ``` HttpCurl::post('http://api.example.com/?a=123', array('abc'=>'123', 'efg'=>'567'), 'json'); HttpCurl::post('http://api.example.com/', '这是post原始内容', 'json'); 文件post上传 HttpCurl::post('http://api.example.com/', array('abc'=>'123', 'file1'=>'@/data/1.jpg'), 'json'); ```
    public static function post($url, $fields, $data_type = 'text')
    {
        $cl = curl_init();
        if (stripos($url, 'https://') !== FALSE) {
            curl_setopt($cl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($cl, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($cl, CURLOPT_SSLVERSION, 1);
        }
        curl_setopt($cl, CURLOPT_URL, $url);
        curl_setopt($cl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($cl, CURLOPT_POST, true);
        curl_setopt($cl, CURLOPT_POSTFIELDS, $fields);
        $content = curl_exec($cl);
        $status = curl_getinfo($cl);
        curl_close($cl);
        if (isset($status['http_code']) && $status['http_code'] == 200) {
            if ($data_type == 'json') {
                $content = json_decode($content);
            }
            return $content;
        } else {
            return FALSE;
        }
    }

Usage Example

Beispiel #1
0
 /**
  * 长链接转短链接接口
  *
  * @string $long_url 需要转换的长链接,支持http://、https://、weixin://wxpay 格式的url
  *
  * @return array(err, data)
  * - `err`, 调用失败时得到的异常
  * - `res`, 调用正常时得到的对象
  *
  * Examples:
  * ```
  * list($err, $data) = $api->shorturl('http://me.diary8.com/category/web-front-end.html');
  * echo $data->short_url;
  * ```
  * Result:
  * ```
  * http://w.url.cn/s/ABJrkxE
  * ```
  */
 public function shorturl($long_url)
 {
     $url = self::API_DOMAIN . 'cgi-bin/shorturl?access_token=' . $this->get_access_token();
     $xml = '{"action":"long2short","long_url":"' . $long_url . '"}';
     $res = HttpCurl::post($url, $xml, 'json');
     // 异常处理: 获取时网络错误
     if ($res === FALSE) {
         return Error::code('ERR_GET');
     }
     // 判断是否调用成功
     if ($res->errcode == 0) {
         return array(NULL, $res);
     } else {
         return array($res, NULL);
     }
 }
All Usage Examples Of Gaoming13\WechatPhpSdk\Utils\HttpCurl::post
HttpCurl