Why does CURL behave strangely

Hello house,

Please i need clarification on why curl returntransfer is acting wild.

I don’t want to get output or response from the page or url i made a curl post request to, so the idea is to set CURLOPT_RETURNTRANSFER, false); using boolen false

However i noticed
CURLOPT_RETURNTRANSFER, false);
does not work as output are returned.

But using string false like this
CURLOPT_RETURNTRANSFER, ‘false’);

This works and no output is returned

Why is it behaving like this instead of the recommended using boolen

Because you have fundamentally misunderstood what the option does, and are using it backwards.

RETURNTRANSFER true says “Dont output the result, return it from curl_exec in a string.”
RETURNTRANSFER false (default) echoes the result.

The option is working correctly - when you give it false, it output the result. When you give it a non-empty string, it translates it into a boolean (any non-empty, non-zero string is boolean TRUE) and acts accordingly.

1 Like

for once i never saw it in this direction, my understanding was that when i use true it means return or get the output. but when i use false it means i don’t want you to get the output.T

Now you have clarified it:

  1. The data contains output of the external site by default regardless if I want the data there or not, is there already.

  2. Returntransfer is only asking me how i want the data already contained in my request to be served to me.
    TRUE : pass it into a variable or just use return captured output.
    FALSE: echo captured output

So if I use TRUE it means i can choose to echo it out by myself or just let it be.

Thanks greatly i highly appreciate your guide

Now my second question is what can be done so that request won’t bother picking output or content from external site at, just make the post and ignore what ever output or content the site has

A cURL request will always return SOMETHING - the response from the server is automatic and part of the protocol. But what it returns is only what the endpoint server outputs as its response body - if it’s HTML output, it will return the HTML. At that point, it hasnt pulled anything other than that HTML. Some HTTP requests return empty response bodies, most dont.

I must go home with something for visiting the house? i understand perfectly.u
My only concern is if the third party site has error messages outputted or virtually anything on the outer space is picked by the request.

Your car will have more mileage on it (bandwidth), and you will have the knowledge that you visited the house (the response code telling you your request was received and processed). So yes, you are going home with something, always.

1 Like

Such a generous giver smile:

// endpoint.php
if($post === 'getbook'){
echo 'Thanks for coming';
exit();
}

That above is okay
but if in endpoint.php there are unsurpressed errors or anything at all that is outputted request will just pack it all and put in my cars trunk heading home.

I just wish am limited to what ever echo that is wrapped inside if($post === ‘getbook’) and allow endpoint.php to settle what ever error or dispute its having with php and not rope me in

Well if the endpoint is coded correctly, what it would do is (pseudocode to follow):

try {
   doStuff();
   doDangerousStuff();
} catch(DangerousStuffException e) {
  http_response_code(500); //Send the response code indicating something went wrong.
  die(1); //Exit PHP Abnormally, to trigger error logging.
} catch (BadInputException e) {
  http_response_code(400); //Response code
  echo "The following error occured: ".e['message']; //User message: There was some bad input, so the user should be informed of how to fix it.
  die(1); //Exit PHP Abnormally.
}

Sounds more professional and a work around it.

However the best would have been for php to introduce a function called http_response_handshake which will be used in endpoint.php like this

if($post === 'getbook'){
http_handshake('Thanks for coming');
exit();
}

So request can only return anything sent from the handshake and ignore everything else.
It doesn’t make sense that I came to get a book from a friend and meet the parents fighting and all their torn clothes litred everywhere and I have to pack all their torn litred clothes including left over foods of their dogs and put in my cars trunk heading home :joy::joy::joy::joy:

All you’ve done is replace the word ‘echo’ with http_handshake.
You still sent back a HTTP code (200, the default);
You still sent back a response body (“Thanks for coming”).

1 Like

But you ommited picking up of garbages,
So if nothing is found in the http_response_handshake i will gladly go home with 200 response code which I will present to my supervisor that i visited my friend but he was not around.

So if there were no crisis in the house i go home with the book or my friend telling me the book is not available empty. plus a $200 tfare back home.

Then if house if on crisis i go home with $200 tfare back home and an empty trunck.

Instead of garbage packing.
Request will only look for anything inside http_response_handshake either empty or full

200 says your friend was home, you shook hands, and left.

This is also 200, but with a bigger response body.

This is 400 (if the crisis is your doing) or 500 (if it’s the server’s doing).

The REQUEST doesnt know anything until it gets a response. This is where the analogy falls down a bit.

Instead of it being you driving to your friend’s house, it’s more like you sending a money request to your friend via pidgeon.
You wrap the message around the bird’s leg, and you send it off (the request).
You have no idea if your friend got it, until you get a response: The bird came back, or it didnt.
If the bird came back, you can check the response message wrapped around its leg by your friend. That message could be anything: 200 (The money is attached),400 (You forgot to tell your friend how much to send), 404 (Someone sends a message that you sent the bird to the wrong house), 500 (Your friend doesnt have that much money to send you), 307 (Your friend tells you to ask another friend), 301 (Someone has wrapped a message around the leg saying your friend has moved)… etc.

much precisely, 500 only occurs when there was a road block and I couldn’t get to the house, maybe a dead body was found and police barricade the place (shut down error) so the police were kind enough to give me $500 tfare back home.

But in the scenario were i reached the house and they opened the door and saw litred clothes and garbages like undefined $variables torn jeans which is not necessary a shut down error.

I pack it and put in my trunk heading home

Nope. Because in order to get the 500 message, the end server has to receive the message, try to handle it, and fail. If you hit a road block, you would just never get a response, and your browser would time out. The request has no concept of “turn back”. It’s an autonomous vehicle, that will either reach its destination, or run out of gas trying.

1 Like

okay so what happens to certain none shut down errors that am being forced to go home with

It’s still a response.

If the communication reaches the endpoint server, it will send a response. That response may, or may not, have a body, but it will always have a status code, because that’s part of the HTTP protocol.

Some servers will use, for example, the HTTP verb DELETE (like POST, and GET are verbs), will delete a resource on the server, and send back a status message (usually 202 or 204) that translates to “I did that”. It doesnt need a body, it’s just the server responding saying the request is complete.

But most times they are not aware of these errors so won’t have made plans of sending me a 204 or deleting the body.

Thats why JavaScript won the peaceful language award 2024.

Because if you send Ajax driver on such errands and he founds anything abnormal at all on the site it will return peacefully empty. doesn’t get involved in street fights like php http request

Javascript has nothing to do with this. An HTTP request is an HTTP request. It doesnt matter what language you use to make it.

If the AJAX request goes to a PHP page, and the PHP page generates errors that get echoed to the output, the AJAX request wont return empty, it will return with whatever was in the output.

Are you certain about this? i will retry this at night and give you feedback. most of my ajax calls fails or returns empty whenever it sees errors in php.
most especially when working with WordPress.

I will need to retest this