-1
foreach ($inventories['rgDescriptions'] as $key => $description){

$samik = $description['app_data'];
$url[] = "https://api2.prices.tf/prices/".$samik["def_index"]."%3B".$samik["quality"];
}
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$headers = array(
   "accept: application/json",
   "Authorization: Bearer " .$value. " ",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);

how can i run this code without having curl_init() expects parameter 1 to be string, array given

  • If it's a lot of data, then it's going to take time. There really is no way around it. You can add a [timeout to cURL](https://stackoverflow.com/questions/2582057/setting-curls-timeout-in-php) so that it won't get stuck for 30 seconds or more trying to connect, but if it does take that long, you won't get the data you need. – aynber Apr 04 '22 at 16:48
  • If the cURL request takes time, and you need to run it multiple times, why not ask whichever API you use for performance improvements on their side? – Nico Haase Apr 04 '22 at 16:50
  • multi_curl can _sometimes_ speed up parallel requesting.. but you query the same URL, not sure if that could become a problem. – Honk der Hase Apr 04 '22 at 17:29
  • even if I do timeout curl_setopt($curl, CURLOPT_TIMEOUT, 1); it takes a lot of time. Can I do foreach just for the url and then use them outside of the foreach? – Sam Čadek Apr 04 '22 at 17:57
  • What do you mean by "foreach just for the url"? – Nico Haase Apr 04 '22 at 18:00
  • If you have a lot of items you're getting prices for, I'd suggest just using the paginated `/prices`, get all those values, then compare to your own list. If you have more than total number of pages, it would take less time. – aynber Apr 04 '22 at 18:03
  • i cant paginate all there is limit for 100 foreach ($inventories['rgDescriptions'] as $key => $description){ $samik = $description['app_data']; $url = "https://api2.prices.tf/prices/".$samik["def_index"]."%3B".$samik["quality"]; } and then use the url to the curl – Sam Čadek Apr 04 '22 at 18:20

1 Answers1

-2

If you work with a framework, you better use a queue system. And while you're at it, a good framework can also do the heavy lifting of curl for you... If you don't have a good framework, Laravel might be a good start.

SomeOne_1
  • 808
  • 10
  • 10
  • That still won't help if the API endpoint is slow – Nico Haase Apr 04 '22 at 17:05
  • No, but it will allow you to be more flexible. You could do more calls at the same time, you have flexibility when it comes to failures. Now if your code fails, you have to start all over again. – SomeOne_1 Apr 04 '22 at 17:11
  • 1
    This is more of a comment than an answer, since it just gives a suggestion instead of an outright fix. – aynber Apr 04 '22 at 17:21
  • depends how you look at it. If you want me to fix a slow API I do not control, then yes, it is a suggestion. The only one who can increase the speed of that API are the owners. Sam can only find solutions to help him go a little faster. and then this is `an outright fix`. – SomeOne_1 Apr 04 '22 at 17:32