致命错误:/var/www/vhosts/abc.com/中的消息'无法连接到主机'的未捕获异常

致命错误:/var/www/vhosts/abc.com/中的消息'无法连接到主机'的未捕获异常

问题描述:

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();
}

我使用Curl连接服务器并使用以下函数更新数据。 该功能运行良好,可更新约400条记录。 之后它会抛出一个错误,请告知如何解决这个问题? strong> p>

 致命错误:未捕获的异常消息'无法连接到主机'在/中 var / www / vhosts / abc.com / 
 
comm_Api_Connection-&gt; 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):
nnmm_Api::updateProduct('5250',Array)#/ {{}}引自/var/www/vhosts/abc.com/httpdocs/mysite/demo/comm- 第204行上的common1 / Api.php 
  code>  pre> 
 
 

PHP函数如下 strong> p>

 &lt;?php 
public function put($ url,$ body)
 {
 $ this-&gt; addHeader('Content-Type',$ this-&gt; getContentType()); 
 
 if  (!is_string($ body)){
 $ body = json_encode($ body); 
} 
 
 $ this-&gt; initializeRequest(); 
 
 $ handle = tmpfile(); 
 fwrite  ($ handle,$ body); 
  fseek($ handle,0); 
 
 curl_setopt($ this-&gt; curl,CURLOPT_INFILE,$ handle); 
 curl_setopt($ this-&gt; curl,CURLOPT_INFILESIZE,strlen($ body)); 
 curl_setopt  ($ this-&gt; curl,CURLOPT_CUSTOMREQUEST,'PUT'); 
 curl_setopt($ this-&gt; curl,CURLOPT_URL,$ url); 
 curl_setopt($ this-&gt; curl,CURLOPT_PUT,true); 
  curl_exec($ this-&gt; curl); 
 
返回$ this-&gt; handleResponse(); 
} 
  code>  pre> 
  div>

At a pinch I'd say you are opening too many connections to the server

Each time you call that method you open a new request, but don't close it. It will stay open until the timeout is reached

If your server only allows 400 simultaneous connections, anything after the 400th call to the method will fail

You need to close your connections after each request

curl_close($ch)

I think your curl need a SSL verification.

Please put following line on your CURL code

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

This line is pass "true" is your request URL is HTTPS otherwise it's "false".

In your case i think it's true. Please pass true parameter and check it.