MoFish Posted July 26, 2021 Share Posted July 26, 2021 Hello, I am trying to do multiple curl requests and get the response for each one output to the screen. I have wrote the following code, but only appear to be getting the last response when outputting $result. What am i doing wrong? Is there a way to wait for each curl request to be completed before appending to $result so I can see them all? Thanks very much if (isset($_POST['submit'])) { $result = array(); foreach ($textAr as $line) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true ); curl_setopt($ch, CURLOPT_ENCODING, "gzip,deflate"); $result[] = curl_exec ($ch); } curl_close ($ch); echo "<pre>"; print_r($result); echo "</pre>"; } Quote Link to comment Share on other sites More sharing options...
gw1500se Posted July 26, 2021 Share Posted July 26, 2021 (edited) What is the URL returning? If it is returning JSON then you need to decode it which results in an array. If so you want to array_merge. Edited July 26, 2021 by gw1500se Quote Link to comment Share on other sites More sharing options...
MoFish Posted July 26, 2021 Author Share Posted July 26, 2021 Hi, It returns a string like the following: string(506) " status error error The domain xxx.co.uk already exists in our database. This may occur if there is a pending Order for xxx.co.uk in our database under your account or any other account." I would like to collate all these return strings for each of the curl request (could be 10+) and return them as an alert/echo at the end of them all. if (isset($_POST['submit'])) { $result = array(); foreach ($textAr as $line) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true ); curl_setopt($ch, CURLOPT_ENCODING, "gzip,deflate"); $result = curl_exec ($ch); curl_close ($ch); var_dump($result); } // i would like all of the strings to be compiled into one for returning to the screen // but it seems like it only gets the last one instead of them all, even if i try string appending. // is there a way to wait until one is completed before the next? } Thank you for your help. Quote Link to comment Share on other sites More sharing options...
requinix Posted July 26, 2021 Share Posted July 26, 2021 The first blob of code you posted was putting those results into an array and looks fine, but I can't help but notice that the request being made inside the loop is going to be the same every time. Shouldn't it be using $line somewhere? Quote Link to comment Share on other sites More sharing options...
MoFish Posted July 26, 2021 Author Share Posted July 26, 2021 (edited) Hi @requinix Apologies i removed that URL bit to simplify it. The $line is passed into the url of the curl call so each is unique. When using the below code and calling 3 curl requests for example; it only gets the last string response even if i try to append. I cannot figure out whats going on! if (isset($_POST['submit'])) { $result = array(); foreach ($textAr as $line) { $url = "https://xxx.com/api/domains/meh.xml?auth-userid=xxx&domain-name={$line}"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true ); curl_setopt($ch, CURLOPT_ENCODING, "gzip,deflate"); $result = curl_exec ($ch); // $result[] = curl_exec ($ch); curl_close ($ch); print_r($result); } // print_r($result); } Edited July 26, 2021 by MoFish Quote Link to comment Share on other sites More sharing options...
MoFish Posted July 26, 2021 Author Share Posted July 26, 2021 xxx1.com xxx2.com Array ( [0] => [1] => status error error The domain xxx2.com already exists in our database. This may occur if there is a pending Order for xxx2.com in our database under your account or any other account.) As you can see, its only getting the result from the last one for some reason. Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted July 26, 2021 Share Posted July 26, 2021 Are you positive that you are sending three requests? Or maybe some of the requests are not returning any results? Consider adding printf('$line: %s'.PHP_EOL, $line); write before you dump the results. Also a reality check printf('$textAr: %s'.PHP_EOL, json_encode($textAr); might not hurt. Quote Link to comment Share on other sites More sharing options...
MoFish Posted July 26, 2021 Author Share Posted July 26, 2021 (edited) @NotionCommotion $line: www.xxx2.com $textAr: ["www.xxx1.com\r","www.xxx2.com"] Array ( [0] => [1] => status error error The domain xxx2.com already exists in our database. This may occur if there is a pending Order for xxx2.com in our database under your account or any other account. You may search for this domain within your control panel.) $result = array(); foreach ($textAr as $line) { $url = "https://example.com/api/domains/blah.xml?domain-name={$line}"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true ); curl_setopt($ch, CURLOPT_ENCODING, "gzip,deflate"); $result[] = curl_exec ($ch); curl_close ($ch); } printf('$line: %s'.PHP_EOL, $line); printf('$textAr: %s'.PHP_EOL, json_encode($textAr)); print_r($result); Edited July 26, 2021 by MoFish Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted July 26, 2021 Share Posted July 26, 2021 I meant something like the following: if (isset($_POST['submit'])) { printf('$textAr: %s'.PHP_EOL, json_encode($textAr)); $result = array(); foreach ($textAr as $line) { $url = "https://example.com/api/domains/blah.xml?domain-name={$line}"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true ); curl_setopt($ch, CURLOPT_ENCODING, "gzip,deflate"); printf('$line: %s'.PHP_EOL, $line); $r = curl_exec ($ch); print_r($r); $result[] = $r; curl_close ($ch); } print_r($result); } Quote Link to comment Share on other sites More sharing options...
MoFish Posted July 26, 2021 Author Share Posted July 26, 2021 (edited) $textAr: ["www.google.com\r","www.yahoo.com"] $line: www.google.com $line: www.yahoo.com status error error Domain yahoo.com already registered Array ( [0] => [1] => status error error Domain yahoo.com already registered ) Its like the second request blanks out the first. If i do three requests, i only ever get the last one. $textAr: ["www.google.com\r","www.yahoo.com\r","www.bbc.com"] $line: www.google.com $line: www.yahoo.com $line: www.bbc.com status error error Domain bbc.com already registered Array ( [0] => [1] => [2] => status error error Domain bbc.com already registered ) Edited July 26, 2021 by MoFish Quote Link to comment Share on other sites More sharing options...
Solution NotionCommotion Posted July 26, 2021 Solution Share Posted July 26, 2021 Okay, $textAr includes URLs for www.google.com\r and www.yahoo.com. Do you really want the \r with Google? Then I see $line: www.google.com being echoed, but don't see the results being echoed. Replace print_r with var_dump as it likely is returning NULL. Then I see $line: www.yahoo.com followed by the error message. I would have expected a line break but not a big deal. Then I see the $results array with the first element being empty and the second being the error response. Isn't this what you were expecting? Consider changing $result[] = $r; to $result[$line] = $r; to make it more clear. Quote Link to comment Share on other sites More sharing options...
MoFish Posted July 27, 2021 Author Share Posted July 27, 2021 @NotionCommotion I was hoping for something like this, as each of these are already registered. Quote Array ( [www.google.com ] => status error error Domain google.com already registered [www.bbc.com ] => status error error Domain bbc.com already registered [www.sky.com] => status error error Domain sky.com already registered ) However, only get the last response Quote $textAr: ["www.google.com\r","www.bbc.com\r","www.sky.com"] $line: google.com $line: bbc.com $line: sky.com status error error Domain sky.com already registered Array ( [www.google.com ] => [www.bbc.com ] => [www.sky.com] => status error error Domain sky.com already registered ) Quote Link to comment Share on other sites More sharing options...
MoFish Posted July 27, 2021 Author Share Posted July 27, 2021 (edited) @NotionCommotion You are correct, some nulls are coming back but i don't understand why there would be no response message with them. $textAr: ["www.google.com\r","www.bbc.com\r","www.sky.com"] $line: google.com bool(false) $line: bbc.com bool(false) $line: sky.com string(203) " status error error Domain sky.com already registered " array(3) { ["www.google.com "]=> bool(false) ["www.bbc.com "]=> bool(false) ["www.sky.com"]=> string(203) " status error error Domain sky.com already registered " } Edited July 27, 2021 by MoFish Quote Link to comment Share on other sites More sharing options...
MoFish Posted July 27, 2021 Author Share Posted July 27, 2021 @NotionCommotion Thank you for all your help, i fixed the issue. It was the \r as you suggested which was causing the issue. Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted July 27, 2021 Share Posted July 27, 2021 3 hours ago, MoFish said: It was the \r as you suggested which was causing the issue. When you see something happening which seems impossible, make you script as small as possible, and every time you will find it was impossible not to happen Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.