PHP code example of snowlyg / wechatpay-guzzle

1. Go to this page and download the library: Download snowlyg/wechatpay-guzzle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

snowlyg / wechatpay-guzzle example snippets


     // $filepath 图片地址
     $imginfo     = pathinfo($filepath);
     $picturedata = file_get_contents($filepath);
     $sign        = hash('sha256', $picturedata);
     $meta        = [
          "filename" => $imginfo['basename'],
           "sha256"   => $sign,
      ];

     $filestr = json_encode($meta);

     public function getBody($filestr, array $imginfo, $picturedata)
     {
         $boundarystr = "--{$this->boundary}\r\n";
         $out         = $boundarystr;
         $out         .= 'Content-Disposition: form-data; name="meta";'."\r\n";
         $out         .= 'Content-Type: application/json; charset=UTF-8'."\r\n";
         $out         .= "\r\n";
         $out         .= "".$filestr."\r\n";
         $out         .= $boundarystr;
         $out         .= 'Content-Disposition: form-data; name="file"; filename="'.$imginfo['basename'].'";'."\r\n";
         $out         .= 'Content-Type: image/'.$imginfo['extension'].';'."\r\n";
         $out         .= "\r\n";
         $out         .= $picturedata."\r\n";
         $out         .= "--{$this->boundary}--\r\n";
         return $out;
     }

    public function getEncrypt($str)
    {
        $public_key = file_get_contents($this->wechat_public_cert);
        $encrypted = '';
        if (openssl_public_encrypt($str, $encrypted, $public_key, OPENSSL_PKCS1_OAEP_PADDING)) {
            $sign = base64_encode($encrypted);
        } else {
            throw new Exception('encrypt failed');
        }

        return $sign;
    }