我尝试发布数据时使用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);
卷曲:(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。