【问题标题】:failed to open stream: Connection refused php无法打开流:连接被拒绝 php
【发布时间】:2020-09-13 12:24:34
【问题描述】:

我有一些这样的http请求

$url = 'https://example.com/authentication';
$data = array('email' => 'value1', 'password' => 'value2');

// use key 'http' even if you send the request to https://...
$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($url, false, $context);
if ($result === FALSE) { /* Handle error */ }

var_dump($result);

但是我遇到了这样的错误 警告:file_get_contents(https://example.com/authentication):无法打开流:连接被拒绝

我读到 curl 可以提供帮助,但我不知道如何实现它,在此先感谢

【问题讨论】:

  • 这不是客户端错误,它与服务器有关。请在终端中运行此命令telnet SERVER_IP_OR_URL 443

标签: php curl https


【解决方案1】:

为基本的 curl 请求尝试这个

$data = array('email' => 'value1', 'password' => 'value2');
$contentType = 'application/x-www-form-urlencoded';
$HTTPCustomHeaders[] = 'Content-Type: '.trim($contentType);
$runfile = 'example.com';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $runfile);
curl_setopt($ch, CURLOPT_HTTPHEADER, $HTTPCustomHeaders);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$content = curl_exec ($ch);
curl_close ($ch); 
echo $content

【讨论】:

    猜你喜欢
    • 2014-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-14
    • 2013-03-27
    • 2015-11-15
    • 1970-01-01
    • 2015-06-12
    相关资源
    最近更新 更多