code码不可泄露! 第一次发现封号7天,第二次发现封号30天,第三次发现永久封号!改图吧保留最终解释权!
免费版:每天限制调用5次; 会员包月版:收费100¥/月,每天不限制!

sample.php

<?php

if (!session_id()) @ session_start();//这里demo把token写入session缓存,也可以写入db、memcache、redis,自行决定。
if (!isset($_SESSION["token"])) {
    $token = get_token_cache();
}
if (time() - $_SESSION["time"] > 7000) {//过了7000秒重新向服务器获取
    $token = get_token_cache();
} else {
    $token = $_SESSION["token"];
}

function get_token_cache()
{
    $getTokenUrl = 'http://api.gaitu8.com/api/v1/token/user';
    $type = ['Content-Type: application/json'];
    $code = json_encode(['code' => 'abcdefghijklmnopqrst']);//****** 获取的code替换这里 ******
    $token = to_curl($getTokenUrl, $type, $code);
    $token_arr = json_decode($token, true);
    $token = ['token:' . $token_arr['token']];
    $_SESSION["token"] = $token;
    $_SESSION["time"] = time();
    return $token;
}

$content = '  我与父亲不相见已二年余了,我最不能忘记的是他的背影。……”';
$content = [
    'txt' => $content,//文字必填
    //'txt_color' => 'BC221C',//文字颜色可选
    //'bg_color' => 'DCF2F4',//背景颜色可选
    //'font_size' => '12',//文字大小可选
    'ad_txt' => '由改图吧API生成',//广告语可选
];
$txt2ImgUrl = 'http://api.gaitu8.com/api/v1/text';
$response = to_curl($txt2ImgUrl, $token, $content);
$response = json_decode($response, true);
var_dump($response);//array result

function to_curl($url, $header, $content)
{
    $ch = curl_init();
    if (substr($url, 0, 5) == 'https') {
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);
    }
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
    $response = curl_exec($ch);
    if ($error = curl_error($ch)) {
        die($error);
    }
    curl_close($ch);
    return $response;
}

?>