1

I have output from an array I would like to use as input in a PHP Curl request. Do I store them as another array and loop through the array with the Curl request?

Here is the output from the array:

foreach ($threadsarray['threads'] as $thread) {   
    print $thread['id']."<br />";
}

These are values I would like to use as input for Curl (obviously these values are different every time depending on the output for each loop above):

178369845
291476958
224408290
270960091
270715888
270513013
229639500
229630641
215503057
214314923

I want to execute a curl request for each of those thread id's... Here is how I am building the Curl request:

$url2 = 'https://api.website.com/endpoint';

    $data2 = array (
        'specialkey' => '123abcd789xyz',
        'anotherparam' => 'Brown',
        'locale' => 'en-US',
        'thread_id' => array (
                       $thread['id']
                       )
        );

        //build the query string because this is a get request
        $params2 = '';
        foreach($data2 as $key2=>$value2)
                $params2 .= $key2.'='.$value2.'&';

        $params2 = trim($params2, '&');


        // Excecute the curl request
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url2.'?'.$params2 );
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 'false');
        $mycurlresult = curl_exec($ch);
echo '<pre>';
$resultarray = json_decode($mycurlrequest, TRUE);
print_r($resultarray);
echo '</pre>';
        if (FALSE === $mycurlrequest)
            throw new Exception(curl_error($ch), curl_errno($ch));

I can't seem to build the request string correctly...what am I missing?

8
  • What errors are you getting? Commented May 1, 2017 at 17:53
  • I'm getting Array to string conversion on the following line:$params2 .= $key2.'='.$value2.'&'; Commented May 1, 2017 at 17:55
  • 2
    Do you know about http_build_query? Commented May 1, 2017 at 17:57
  • You're getting that array to string conversion because one of the $value2s in $data2 is an array. Commented May 1, 2017 at 17:59
  • Maybe you just want $thread['id'] instead of array($thread['id'])? Commented May 1, 2017 at 18:01

1 Answer 1

4

I can't really test this, but I'd suggest something like this. First, set up your curl, and create an array with an empty placeholder for thread_id.

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 'false');

$url2 = 'https://api.website.com/endpoint';
$data2 = array(
    'specialkey' => '123abcd789xyz',
    'anotherparam' => 'Brown',
    'locale' => 'en-US',
    'thread_id' => ''
);

Then loop over your array. For each item, replace the thread_id key in the $data2 parameters array with that item's id, build the query using http_build_query and execute the request.

foreach ($threadsarray['threads'] as $thread) {
    $data2['thread_id'] = $thread['id'];                  // add the current id
    $params2 = http_build_query($data2);                  // build the new query
    curl_setopt($ch, CURLOPT_URL, $url2.'?'.$params2 );
    $mycurlresult = curl_exec($ch);
    echo '<pre>';
    $resultarray = json_decode($mycurlrequest, TRUE);
    print_r($resultarray);
    echo '</pre>';
    if (FALSE === $mycurlrequest)
        throw new Exception(curl_error($ch), curl_errno($ch));
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you sir. You nailed it. Much appreciated!!
Instead of this foreach loop creating 10 different arrays as output, is there a way to get the output into 1 array? Or is there an easy way to step through each of the 10 arrays after the foreach completes?
Sure, just append to $resultarray instead of overwriting it. $resultarray[] = json_decode($mycurlrequest, TRUE);.
Ah, yes. But it's still printing each array as it's appending. I'm after the last array (the total combined array). Is there a way to only print that one?
Yep, just move your print to after the loop instead of inside it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.