не удалось открыть поток: в соединении отказано php - PullRequest
0 голосов
/ 26 мая 2020

У меня есть такой 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 может помочь, но я не знаю, как его реализовать, заранее спасибо

1 Ответ

0 голосов
/ 26 мая 2020

попробуйте это для basi c запрос 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
...