Best way to loop requests to a PHP API

Asked

Viewed 215 times

0

I am working with an api in which I send a CURL request and it returns me a json the problem is that if I loop in PHP the server hangs and the processing is very slow, a solution I found was to separate the page that makes the request and to make a request at a time via ajax, but in this case after 3 thousand times the server also hangs. I need some idea so I can make at least 20,000 requests faster and without crashing the server. Anyone have any idea?

Part of the code:

public function put($path, $body = null, $params = array()) {
        $body = json_encode($body);
        $opts = array(
            CURLOPT_HTTPHEADER => array('Content-Type: application/json'),
            CURLOPT_CUSTOMREQUEST => "PUT",
            CURLOPT_POSTFIELDS => $body
        );

        $exec = $this->execute($path, $opts, $params);

        return $exec;
    }

while($i<$t){
    $price = $arr_precos[$i];
    $mlb = $arr_ids[$i];
    $i++;

    $edit = '/items/'.$mlb;
    $body = array("price"=>$price);
    $response = $meli->put($edit, $body, $params);
}

The above code executes the loop, the other way I found using the same code but without the loop by PHP, I loop requests to a PHP file with this code via ajax.

Note. All request and data processing is done in PHP + MYSQL

  • Post the code, so we can get ideas above your context.

  • Thank you, I edited the question to try to make it clearer

  • Does it really lock or throw an exception? I’ve seen people with this problem and it actually reached the execution limit. Give a look at set_time_limit.

  • Man, it’s necessary to know if your problem is of runtime or massive requests to the server, in your code, put it to print every second, if it’s not time out, the problem is the massive requests, you can put a Sleep with thousandths of a second to give a "rest" to the server

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.