0

I am using Curl to connect to a server and update data using the following function. The function works great and updates approx 400 records. After that it throws an error, please advise how to solve this issue ?

Fatal error: Uncaught exception  with message 'couldn't connect to host' in /var/www/vhosts/abc.com/

comm_Api_Connection->put('https://www.vam...', Object(stdClass)) #2 /var/www/vhosts/abc.com/httpdocs/mysite/demo/comm-common1/Api.php(1530): 

comm_Api::updateResource('/products/5250', Array) #3 /var/www/vhosts/abc.com/httpdocs/mysite/demo/sync_prod_inventory_new1.php(1088):

comm_Api::updateProduct('5250', Array) #4 {main} thrown in /var/www/vhosts/abc.com/httpdocs/mysite/demo/comm-common1/Api.php on line 204

The PHP Function is as follows

<?php
public function put($url, $body)
{
    $this->addHeader('Content-Type', $this->getContentType());

    if (!is_string($body)) {
        $body = json_encode($body);
    }

    $this->initializeRequest();

    $handle = tmpfile();
    fwrite($handle, $body);
    fseek($handle, 0);

    curl_setopt($this->curl, CURLOPT_INFILE, $handle);
    curl_setopt($this->curl, CURLOPT_INFILESIZE, strlen($body));
    curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PUT');
    curl_setopt($this->curl, CURLOPT_URL, $url);
    curl_setopt($this->curl, CURLOPT_PUT, true);
    curl_exec($this->curl);

    return $this->handleResponse();
}
4

2 回答 2

1

我认为您的 curl 需要 SSL 验证。

请在您的 CURL 代码上添加以下行

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

此行通过“true”是您的请求 URL 是 HTTPS,否则为“false”。

在你的情况下,我认为这是真的。请传递真实参数并检查它。

于 2014-05-22T11:27:48.950 回答
1

在紧要关头,我会说您打开了太多与服务器的连接

每次调用该方法时,您都会打开一个新请求,但不要关闭它。它将保持打开状态,直到达到超时

如果您的服务器只允许 400 个同时连接,则在第 400 次调用该方法之后的任何操作都将失败

您需要在每次请求后关闭连接

curl_close($ch)
于 2013-04-30T14:03:19.877 回答