I am trying to achieve a simple http request. However, both file_get_contents and cURL are not working as expected
For cURL: it is taking too long to load and eventually timed out (I couldn't even display the curl info)
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, array('grant_type' => 'authorization_code', 'redirect_uri' => $redirect_uri, 'code' => urlencode($_GET['code'])));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array(
"Accept: */*",
"Accept-Encoding: gzip, deflate, br",
"Content-Type: multipart/form-data; charset=UTF-8",
"Authorization: Basic " . base64_encode("username:password"),
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_ENCODING, '');
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
if($resp === false)
{
echo 'Curl error: ' . curl_error($curl);
}
$info = curl_getinfo($ch);
print_r($info);die;
curl_close($curl);
var_dump($resp);die;
UPDATE: I've tried adding this line as mentionned in the comments below:
curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
Now getting this error: OpenSSL SSL_read: Connection reset by peer, errno 104
Even thought in the code i've set the ssl verify peer to false
For file_get_content: a warning is displayed: failed to open stream: HTTP request failed!
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$context = stream_context_create([
"http" => [
"method" => "POST",
"header" => "Authorization: Basic YWRzZ2x3X3l9zdGFnZTpRUDBDVDtcFRs",
],
"ssl" => [
"verify_peer" => false,
"verify_peer_name" => false,
]
]);
$result = file_get_contents($url, false, $context);
print_r($result);die;