1

i get no result? No error, no feedback...just nothing. What's wrong? How can I debug this?

When I run this CURL in console, everything is okay.

PHP 7.4.21

$response = get_curlresult();

$resArr = array();
$resArr = json_decode($response);
echo "<pre>"; print_r($resArr); echo "</pre>";

function get_curlresult() {

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, 'https://www.lorem.ipsom');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');


    curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');

    $headers = array();
    $headers[] = 'Host: api.ipsum.com';
    $headers[] = 'Cookie: ASP.NET_SessionId=fsdflkj34jlka';
    $headers[] = 'Accept: application/json';
    $headers[] = 'Content-Type: application/json';
    
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    $result = curl_exec($ch);

    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    } 

    curl_close ($ch);

    return $result;

}

Console cmd:

curl -H "Host: api.ipsum.com" -H "Cookie: ASP.NET_SessionId= fsdflkj34jlka" -H "accept: application/json" -H "content-type: application/json" --compressed "https://www.lorem.ipsom" 
Noe
  • 11
  • 2
  • Please show the console curl command you used, then we can first make sure they are 100% equivalent. You can [edit] your question when you need to add more info. Also, what exact result do you get in the console version? – ADyson Jul 31 '22 at 20:09
  • You are requesting `https://www.lorem.ipsom` which does not exist – Jacob Mulquin Jul 31 '22 at 20:17
  • @ADyson Good point. Added the cmd. – Noe Jul 31 '22 at 20:20
  • @JacobMulquin I changed, because it's confident – Noe Jul 31 '22 at 20:21
  • I tired your command and got this: `curl: (6) Could not resolve host: www.lorem.ipsom` – Jacob Mulquin Jul 31 '22 at 20:21
  • 1
    You mean "confidential"? – ADyson Jul 31 '22 at 20:22
  • In the console version there's a space after the = in the cookie header. I doubt it's significant but it's a difference. Also are you sure the console version uses the same compression method? – ADyson Jul 31 '22 at 20:24
  • Also you could try debugging the request in more detail as described here: https://stackoverflow.com/a/14436877/5947043 – ADyson Jul 31 '22 at 20:26
  • Also check the returned http headers instead of just the body – ADyson Jul 31 '22 at 20:26
  • https://stackoverflow.com/questions/9183178/can-php-curl-retrieve-response-headers-and-body-in-a-single-request – ADyson Jul 31 '22 at 20:27
  • Don't quote me on this but isn't php a bit funny when it comes to sending headers after there has been output. I know that the curl session is not the same as the session serving the script but could it be that echo command at the top of the page which is ultimately causing the issue. Do the curl (with its headers) before doing any output to the page? – Stephen Duffy Aug 01 '22 at 03:32
  • @StephenDuffy those are request headers, not response headers, they don't go to the browser, so no it's not vulnerable to the "headers already sent" error in this case, that's not part of the issue here – ADyson Aug 01 '22 at 06:35

0 Answers0