B
B
buryakov_dima2015-11-27 16:02:05
PHP
buryakov_dima, 2015-11-27 16:02:05

Why doesn't cUrl run in a loop?

Hello, I'm asking for help with solving one small problem with the cycle and cUrl. There is the following code:

public function getAllMessage($offset = 0)
    {
        $groupCounter = 0;
        foreach ($this->groupArray as $currentGroup)
        {
            $groupCounter++;

           $apiUrl = "https://api.vk.com/method/wall.get?owner_id=-". $currentGroup ."&count=1";

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $apiUrl);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_TIMEOUT, 10);

            $apiResponse = curl_exec($ch);
            $outputArray[$groupCounter] = json_decode($apiResponse, true);

            curl_close($ch);
        }
        print_r($outputArray);
    }

The problem is that the URLs for cUrl are passed in a foreach loop (I also tried the usual for), for example, if you submit an array with a URL consisting of 3 elements as an input, then logically, the output should be an array that will contain 3 responses from the server VK, but in reality it turns out that the first 2 elements of the array are generally empty, but just in the last one there is a response from the VK server (and this response comes from the third, last element of the input array, i.e. the last link). What's wrong? Why doesn't it work, please help!
ps all URLs are valid (checked)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Anton Shamanov, 2015-11-27
@SilenceOfWinter

longer timeout, check $this->groupArray values ​​for validity, it's best to force $outputArray declaration - $outputArray = array();

A
Adamos, 2015-11-27
@Adamos

curl returns false on error.
json_decode from it is useless to do.
And if the argument is in the wrong encoding, it will also be decoded into the void.
Well, the use of $groupCounter demonstrates that this looks like your first PHP script ;)

E
Eugene, 2015-11-27
@Nc_Soft

In VK there are restrictions on the frequency of requests to api, this is 3 times per second (it is written at the very bottom)
https://vk.com/dev/api_requests

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question