dqcd84732 2017-05-01 17:51
浏览 83
已采纳

具有数组值的卷曲请求作为查询(GET)参数的输入

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?

  • 写回答

1条回答 默认 最新

  • dsfds4551 2017-05-01 18:13
    关注

    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));
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算