0

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;
Guest012393
  • 255
  • 1
  • 4
  • 15
  • One common culprit is that DNS resolution on the server itself isn't correct. You could test this by running and timing a simple [`dns_get_record`](https://www.php.net/manual/en/function.dns-get-record.php). If that's the case, the best solution is probably to fix the server's DNS, but you can also do it locally, too. There's some option here: https://stackoverflow.com/q/17814925/231316 – Chris Haas Jan 24 '22 at 18:06
  • Are you aware `username` is spelt wrong in the cURL headers array? – Josh Bonnick Jan 24 '22 at 18:23
  • @ChrisHaas Thank you, I've managed to load the curl, but please see the updated question as now an error regarding the SSL peer is appearing – Guest012393 Jan 25 '22 at 08:01
  • @JoshBonnick in my code it is the actual username and password. Typo is only on the question. Thanks – Guest012393 Jan 25 '22 at 08:01
  • Some people say that you might need to tweak your headers: https://stackoverflow.com/a/53811236/231316 – Chris Haas Jan 25 '22 at 12:50
  • Nothing listed in the mentionned link is working. And i am still struggling with this issue. Do you have any other suggestions? @ChrisHaas – Guest012393 Jan 26 '22 at 12:13

0 Answers0