cURL returning an empty response in PHP - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Wednesday, September 27, 2023

cURL returning an empty response in PHP

I'm reverse engineering an internal API so I can easily retrieve data from a website with a PHP class. I've had great success with GET and POST requests so far. However, there's this specific POST request I've been troubleshooting for the past few days.

When I send that exact request through Burp's repeater, I get a response with content-lenght of 1308, whereas when sending the same request with cURL in PHP, I get a content-lenght of 0.
Both return 200 OK.

Burp POST request:

POST /cgi-bin/api.exe/path HTTP/1.1
Host: xxx.domain.tld
Cookie: session=XXXXXXXXXXX
Content-Length: 22

{"action":"get_data"}

cURL PHP request:

public function getData($domain, $session){
       // URL of the target domain
       $url = "https://$domain/cgi-bin/api.exe/path";

        // Payload data
        $payload = json_encode([
            "action" => "get_data"
        ]);
    
        // cURL initialization
        $ch = curl_init($url);
    
        // Set cURL options
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return the response as a string
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Follow redirects
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Disable SSL verification
        curl_setopt($ch, CURLOPT_HEADER, true); // Include headers in the response, debugging purpouses
        curl_setopt($ch, CURLOPT_COOKIE, "session=$session");
        curl_setopt($ch, CURLOPT_VERBOSE, true);
    
    
        curl_setopt($ch, CURLOPT_POSTFIELDS, $payload); // Set the payload
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Content-Type: application/json', // Set the payload content type
        'Content-Length: ' . strlen($payload) // Set the content length header
        ]);
    
        // Execute the cURL request
        $response = curl_exec($ch);
    
        // Check for cURL errors
        if (curl_errno($ch)) {
            echo 'cURL Error: ' . curl_error($ch);
        } else {
            return $response;
        }
    
        // Close cURL session
        curl_close($ch); 
}

When preforming other POST requests, such as getting the session cookie by posting login data, it returns a normal response just as expected.



source https://stackoverflow.com/questions/77182157/curl-returning-an-empty-response-in-php

No comments:

Post a Comment