Very slow to get remote image solutions, Alipay applet to obtain two-dimensional code

Request Alipay two-dimensional code picture content is very slow solution

Alipay recently developed small projects program, through an interface intended to generate two-dimensional code downloaded to the server, access with file_get_contents, the result is limited to a very slow 7-10 seconds to get to

With img tag access is fast, it is estimated that determine the browser request header

Later the Internet to find a solution that is accessed using curl to simulate a browser. Take seconds

<?php
$camera_url = 'https://mdgw.alipay.com/wsdk/img?fileid=A*OG3WRZIq5S0AAAAAAAAAAAAAAQAAAQ&bz=am_afts_openhome&zoom=original';
$camera_base64 = get_head($camera_url);
if ($camera_base64!='') {
    echo json_encode(array('camera_base64' => $camera_base64),JSON_UNESCAPED_UNICODE);;
}
function get_head($url)
{
    $header = [
        'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0',
        'Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
        'Accept-Encoding: gzip, deflate',
    ];
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_ENCODING, ‘gzip‘);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    $data = curl_exec($curl);
    $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    curl_close($curl);
    file_put_contents('aaa.png',$data);
    if ($ code == 200) {// the URL format images converted into base64_encode format!
        $imgBase64Code = "data:image/jpeg;base64," . base64_encode($data);
        return $ imgBase64Code; // graphic content
    } else {
        return 'to obtain Avatar failed';
    }
}

  

Guess you like

Origin www.cnblogs.com/pxjbk/p/11874493.html