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 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line