【问题标题】:PHP Cache issue when posting data using fsockopen使用 fsockopen 发布数据时的 PHP 缓存问题
【发布时间】:2016-01-18 19:06:17
【问题描述】:

我正在使用 Mailchimp api 1.3 php 包装器。问题是我的客户托管站点的服务器将缓存响应,它甚至不会进行 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,但从缓存中读取总是一样的。

【问题讨论】:

  • 这听起来像是要与您的托管服务提供商讨论。缓存 POST 响应真的非常糟糕。

标签: php caching mailchimp fsockopen


【解决方案1】:

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

    "&time=".time()

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

【讨论】:

  • 我之前加过,并没有解决问题,可能是因为它是POST而不是GET,因此url保持不变。
  • time 放在 URL 中,即使是 POST。
猜你喜欢
  • 2011-01-22
  • 2011-05-22
  • 2011-11-15
  • 2014-02-06
  • 2013-07-16
  • 2010-11-20
  • 1970-01-01
  • 1970-01-01
  • 2012-04-03
相关资源
最近更新 更多