如何解决 cURL 错误 (7): 无法连接到主机?

编程入门 行业动态 更新时间:2024-10-27 22:23:48
本文介绍了如何解决 cURL 错误 (7): 无法连接到主机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用 cUrl(php) 以 xml 格式将项目代码发送到 Web 服务.我在 localhost 中得到了正确的响应,但是它什么时候在服务器上显示

I send an item code to a web service in xml format using cUrl(php). I get the correct response in localhost, but when do it server it shows

cURL 错误 (7):无法连接到主机

cURL Error (7): couldn't connect to host

这是我的代码:

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_error "; } else { echo "Data received "; } curl_close($ch); echo $data; }

我将商品代码发送到理货机并从中获取详细信息.我尝试同时使用 php 4+ 和 php5+ 版本,但没有任何解决方案.

I send the item code to the tally and fetch the details from it. I tried using both the versions php 4+ and php5+, nothing works out Any solution.

推荐答案

CURL 错误代码 7 (CURLE_COULDNT_CONNECT)

CURL error code 7 (CURLE_COULDNT_CONNECT)

非常明确......这意味着无法连接()到主机或代理.

is very explicit ... it means Failed to connect() to host or proxy.

以下代码适用于任何系统:

The following code would work on any system:

$ch = curl_init("google"); // initialize curl handle curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); $data = curl_exec($ch); print($data);

如果您看不到 google 页面,则 .. 您的网址错误 或者您有一些 firewall 或 restriction 问题.

If you can not see google page then .. your URL is wrong or you have some firewall or restriction issue.

更多推荐

如何解决 cURL 错误 (7): 无法连接到主机?

本文发布于:2023-06-05 01:11:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/508785.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何解决   连接到   错误   主机   cURL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!