繁体   English   中英

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

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

我正在尝试发布数据使用 curl & file_get_contents 到 'url1' 但得到一些错误连接被拒绝,但是当将数据发布到 'url2' 时它是正常的。

卷曲:

$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) 无法连接到 url1 端口 82: 连接被拒绝

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

警告:file_get_contents(url1):无法打开流:连接被拒绝

这段代码我弄错了吗?

这个问题似乎与目标服务器或防火墙配置有关。 您可以检查目标防火墙设置或使用目标服务器所需的所有标头。 首先尝试使用并为原始标头添加适当的值。

如果问题不是防火墙,您可以查看https://www.w3.org/wiki/CORS

暂无
暂无

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

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