How to suppress the return of curl_init php?

0

This same function I put in another question for another reason, in this question I want to delete the text that $ result returns:

...    
$fields = http_build_query($data);
$post = curl_init();

$url = $url . '?' . $fields;
curl_setopt($post, CURLOPT_URL, $url);
curl_setopt($post, CURLOPT_POST, 1);
curl_setopt($post, CURLOPT_RETURNTRANSFER, false);
curl_setopt($post, CURLOPT_POSTFIELDS, $fields);

$result = curl_exec($post);

curl_close($post);

I can not seem to find a way to do this:

/ p>

I tried curl_setopt ($ post, CURLOPT_RETURNTRANSFER, false);

But it did not work, I'm starting to use this curl_init now

What I need is simply to know if the $ result is true or false

    
asked by anonymous 25.05.2018 / 01:15

1 answer

1

Use curl_setopt($post, CURLOPT_RETURNTRANSFER, 1) so that the result of the request is not shown or curl_setopt($ch, CURLOPT_NOBODY, 1) if the body of the response is insignificant.

To know whether the request was successful or not, you can add the curl_setopt($ch, CURLOPT_FAILONERROR, 1) so that $result is false if the HTTP Code is> 400. However, the HTTP Code for 300 does not will follow the request, add CURLOPT_FOLLOWLOCATION if needed.

To compare then make a $result !== false :

$fields = http_build_query($data);
$post = curl_init();

$url = $url . '?' . $fields;
curl_setopt($post, CURLOPT_URL, $url);
curl_setopt($post, CURLOPT_POST, 1);
curl_setopt($post, CURLOPT_POSTFIELDS, $fields);

// Acrescentado:
curl_setopt($post, CURLOPT_FAILONERROR, 1)
curl_setopt($post, CURLOPT_RETURNTRANSFER, 1)
curl_setopt($post, CURLOPT_NOBODY, 1)

$result = curl_exec($post);
$info = curl_getinfo($post);
curl_close($post);

if($result !== false){
   echo 'Erro com código de ' . $info['http_code'];
}else{ 
   echo 'Requisição bem sucedida';
}

I'm ignoring potential security issues and have not tested the code.

    
25.05.2018 / 01:36