search

Home  >  Q&A  >  body text

Efficient ways to resolve cURL Error (7): Unable to connect to host

<p>I am using cUrl (php) to send a project code to a web service in xml format. On localhost I get the correct response, but when executed on the server it shows: </p> <blockquote> <p>cURL Error (7): Unable to connect to host</p> </blockquote> <p>Here is my code: </p> <pre class="brush:php;toolbar:false;">function xml_post($post_xml, $url) { $user_agent = $_SERVER['HTTP_USER_AGENT']; $ch = curl_init(); // Initialize curl handle curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_FAILONERROR, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_TIMEOUT, 50); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_xml); curl_setopt($ch, CURLOPT_USERAGENT, $user_agent); // curl_setopt($ch, CURLOPT_PORT, $port); $data = curl_exec($ch); $curl_errno = curl_errno($ch); $curl_error = curl_error($ch); if ($curl_errno > 0) { echo "cURL error ($curl_errno): $curl_errorn"; } else { echo "Received data n"; } curl_close($ch); echo $data; }</pre> <p>I send the project code to tally and get the details from it. I tried using both versions php 4 and php5 with no success. Is there any solution? </p>
P粉674999420P粉674999420506 days ago782

reply all(1)I'll reply

  • P粉208286791

    P粉2082867912023-08-21 12:52:45

    Pretty clear...it means Unable to connect to host or proxy.

    The following code will work on any system:

    $ch = curl_init("http://google.com");    // 初始化curl句柄
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    $data = curl_exec($ch);
    print($data);

    If you can't see the Google page, it's possible that your URL is wrong or you have some firewall or restriction issue.

    reply
    0
  • Cancelreply