0

I have a reasonably simple PHP that runs several curls based on PHP POST values from a form. Once it mangles the POST'd variables as I need it to, it runs 4 curl_init/curl_exec/curl_close in series.

It runs the first 2 curls fine, and prints my output/debug as required. It reaches a third curl, which seems to execute and POST perfectly fine, but doesn't seem to do anything after that. I have basic error capturing (parse JSON response for array/value, if array, echo error, else echo no error).

When no error is encountered with the problem curl, it seems to not continue. However, if there is an error, it continues. Note I am not explicitly telling the code to continue, it's just a very basic "if else" in PHP. This is for debug, and when I find out why it's misbehaving, die will be used to exit. I do not tell it to break or die.

This third, "blocking" curl is the same endpoint as the previous two and returns the same valid JSON as the first two curl calls. I want to know why the script seems to stop when no error is encountered. Is it not continuing due to an open connection? I tried adding "connection close" headers to the problem curl but no difference, as well as a timeout via CURLOPTS.

Below is the code structure used for each cURL.

$curl = curl_init();

echo "<p>Opening curl for <theApplication></p>";

curl_setopt_array($curl, array(
  CURLOPT_URL => '<theEndpoint>',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 1,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS =>'<validJSON>',
  CURLOPT_HTTPHEADER => array(
    'Content-Type: application/json'
  ),
));

$response = curl_exec($curl);
$data = json_decode($response);
$results = $data->result[0];
$maintenanceid = $results->ids[0];

$errors = $data->error->data;
   
if ($errors) {
  echo "<p><font color='red'>Error: ".$errors."</font></p>";
} else {
  echo "<p><font color='green'>No errors!</font></p>";
};

curl_close($curl);

echo "<p>Finished the curl! Why aren't you printing me!</p>";
Phil
  • 157,677
  • 23
  • 242
  • 245
ClontarfX
  • 1
  • 1
  • The code in your question doesn't seem to represent the description above. That aside, are you saying that you don't see the _"Finished"_ message after you see _"No errors!"_? Have you made sure you can [see any and all error messages](https://stackoverflow.com/a/845025/283366)? Have you checked the page source (right click in your browser and select _"View Page Source"_) to make sure it's not just some garbled HTML hiding your message? – Phil Jun 24 '22 at 04:41
  • Then you've probably got an unseen error due to the response not being what you expect. In particular, if the response is not JSON or does not contain a `result` property then your code will fail before it reaches the `if` – Phil Jun 24 '22 at 04:49
  • In fact I won't even see the "No errors" being echoed. I will only see an echo from that if statement when it's true. `error_reporting(E_ALL); ini_set('display_errors', 1);` Setting the above prints errors about undefined property $data, which doesn't make a lot of sense to me as $data is populated by the json_decode($response). Based on your feedback I've gone and echoed $response and $data for the problem curl. $response is correctly populated however echo $data; returns `Fatal error: Uncaught Error: Object of class stdClass could not be converted to string` – ClontarfX Jun 24 '22 at 04:51
  • Use `var_dump($data)` or `print_r($data)` for displaying objects – Phil Jun 24 '22 at 04:53
  • I am an idiot, I just realised I was needlessly putting result into a variable then parsing it for an array again afterwards. After reworking the code to `$maintenanceid = $data->result->maintenanceids[0];` the script continues. I should have paid more attention to the warning/fatal error. Thankyou for pointing me in the right direction :) – ClontarfX Jun 24 '22 at 04:57
  • There's a reason they're _fatal_ – Phil Jun 24 '22 at 04:58

0 Answers0