简体   繁体   English

使用 curl 和 file_get_contents POST 数据:连接被拒绝

[英]POST data with curl & file_get_contents : Connection refused

im trying post data use curl & file_get_contents to 'url1' but get some error Connection Refused, but when post data to 'url2' its normal.我正在尝试发布数据使用 curl & file_get_contents 到 'url1' 但得到一些错误连接被拒绝,但是当将数据发布到 'url2' 时它是正常的。

Curl :卷曲:

$post = ['data1'=>'A','data2'=>'B'];
$ch = curl_init('url1');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);

curl: (7) Failed to connect to url1 port 82: Connection refused curl: (7) 无法连接到 url1 端口 82: 连接被拒绝

file_get_contents: file_get_contents:

$data = array('data1'=>'A','data2'=>'B');
$options = array(
                'http' => array(
                    'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
                    'method'  => 'POST',
                    'content' => http_build_query($data),
                    ),
            );
$context  = stream_context_create($options);
$result = file_get_contents('url1', false, $context);
var_dump($result);

Warning: file_get_contents(url1): failed to open stream: Connection refused警告:file_get_contents(url1):无法打开流:连接被拒绝

did i make mistake with this code ?这段代码我弄错了吗?

This question seems to relate to target server or firewall configuration.这个问题似乎与目标服务器或防火墙配置有关。 You can check the target firewall settings or use all headers required at destination server.您可以检查目标防火墙设置或使用目标服务器所需的所有标头。 First try to use and add a proper value for origin header.首先尝试使用并为原始标头添加适当的值。

If the problem is not the firewall, you can take a look at https://www.w3.org/wiki/CORS .如果问题不是防火墙,您可以查看https://www.w3.org/wiki/CORS

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM