O
O
oe24y2018-09-18 12:50:44
PHP
oe24y, 2018-09-18 12:50:44

PHP CURL: How to re-curl if I get a 503 code (see inside)?

The server with frequent requests gives the 503rd code.
If you repeat after 3 seconds, you can parse again.
This code throws an error (error text below the code):

function get_url($href) {
      $curl = curl_init();
      curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
      curl_setopt($curl, CURLOPT_HEADER, false);
      curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
      curl_setopt($curl, CURLOPT_URL, $href);
      curl_setopt($curl, CURLOPT_REFERER, $href);
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
      curl_setopt($curl, CURLOPT_USERAGENT, "my_useragent_string");
      $str = curl_exec($curl);
      $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
      if ($httpcode == '503') {
      	curl_close($curl);
      	sleep(4);
      	$this->get_url($href);
      }
      curl_close($curl);
            return $str	
  }

Error text:
Message: curl_close(): 38 is not a valid cURL handle

resource
(changing useragent doesn't help)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Burov, 2018-09-18
@oe24y

So 403 or 503?

if ($httpcode == '503') {
      	curl_close($curl);
      	sleep(4);
      	$this->get_url($href);
      }
      curl_close($curl);
you do curl_close twice.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question