Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ZaimArif's avatar

How to retrieve data from API with headers?

First of all sorry for asking such basic question. I'm new to laravel and API and I stuck with this problems for 3 weeks. I've already search the solution online, but no result. The question is how do I connect an API to my project and get the data? I've already try cURL request, but it give me error say that "Failed to connect to roost.mobsocial.net port 443: Connection refused". This is my code:

   $username='myuss';
       $password='mypass';
       $data=$username.$password;
       $secretkey='uss@pass';
       $authentication=hash_hmac('sha256',$data,$secretkey);

        //get method
        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => "https://roost.mobsocial.net/roostClient/",
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_TIMEOUT => 30000,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "GET",
            CURLOPT_HTTPHEADER => array(
                // Set Here Your Requesred Headers
                'Content-Type: application/json',
                'Authentication: '.$authentication
            ),
        ));
        $response = curl_exec($curl);
        $err = curl_error($curl);
        curl_close($curl);

        if ($err) {
            echo "cURL Error #:" . $err;
        } else {
            print_r(json_decode($response));
        }
0 likes
1 reply
D9705996's avatar
D9705996
Best Answer
Level 51

You are trying to connect to https://roost.mobsocial.net/roostClient/ However when I try to connect to this server on port 443 for HTTPS it is closed

nmap -Pn -pT:443 roost.mobsocial.net
Nmap scan report for roost.mobsocial.net (49.236.205.17)

PORT    STATE  SERVICE
443/tcp closed https

However if I try on port 80 for HTTP it is open

nmap -Pn -pT:80 roost.mobsocial.net

Nmap scan report for roost.mobsocial.net (49.236.205.17)

PORT   STATE SERVICE
80/tcp open  http

Therefore if you try changing

CURLOPT_URL => "https://roost.mobsocial.net/roostClient/",

to

CURLOPT_URL => "http://roost.mobsocial.net/roostClient/", 

However bear in mind that any data you send via HTTP will not be encrypted in transit.

You should also have a look at http://docs.guzzlephp.org/en/stable/ as it is a library to help you perform HTTP(S) request from your PHP and will save you having to work with native PHP curl functions.

Please or to participate in this conversation.