使用fsockopen发布数据时出现PHP Cache问题

时间:2015-10-20 08:08:32

标签: php caching mailchimp fsockopen

我正在使用Mailchimp api 1.3 php wrapper。问题是我的客户托管网站的服务器会缓存响应,甚至不会进行api调用。完全相同的代码适用于我和其他客户:

$api = new MCAPI($apiKey);
$doubleOptin = false;          
$mergeVar = array(
 'FNAME' => '',
 'LNAME' => ''
);                     
$api->listSubscribe($listId, $email, $mergeVar, 'html', $doubleOptin);  
print_r($api);  

方法listSubscribe()调用方法callServer我认为是问题所在:

    $payload = "POST " . $this->apiUrl["path"] . "?" . $this->apiUrl["query"] . "&method=" . $method . " HTTP/1.0\r\n";
    $payload .= "Host: " . $host . "\r\n";
    $payload .= "User-Agent: MCAPI/" . $this->version ."\r\n";
    $payload .= "Content-type: application/x-www-form-urlencoded\r\n";
    $payload .= "Content-length: " . strlen($post_vars) . "\r\n";
    $payload .= "Connection: close \r\n\r\n";
    $payload .= $post_vars;

    ob_start();
    if ($this->secure){
        $sock = fsockopen("ssl://".$host, 443, $errno, $errstr, 30);
    } else {
        $sock = fsockopen($host, 80, $errno, $errstr, 30);
    }    
    if(!$sock) {
        $this->errorMessage = "Could not connect (ERR $errno: $errstr)";
        $this->errorCode = "-99";
        ob_end_clean();
        return false;
    }

    $response = "";
    fwrite($sock, $payload);
    stream_set_timeout($sock, $this->timeout);
    $info = stream_get_meta_data($sock);
    while ((!feof($sock)) && (!$info["timed_out"])) {
        $response .= fread($sock, $this->chunkSize); 
        $info = stream_get_meta_data($sock);
    }
    var_dump($response); exit;

有人知道为什么fsockopen和fwrite永远不会向Mailchimp发送电话吗?奇怪的是,我实际上可以读取$ response,但它在缓存中始终是相同的。

1 个答案:

答案 0 :(得分:0)

如果代码适合您并且有一些查询缓存,那么您可以尝试向请求添加时间戳。

    "&time=".time()

这样你总是有一个“新鲜”的请求。