duancan1732
2019-03-28 07:31 阅读 205
已采纳

如何使用&传递curl中的数组?

I made one curl call function it's like below:

public function curl($url, $post = array()){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.12) Gecko/20070508 Firefox/1.5.0.12");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $this->ckfile);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $this->ckfile);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    // curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,2);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch,CURLOPT_URL, $url);
    if(!empty($post)){
        $post_string = "";
        foreach($post as $key=>$value) { $post_string .= $key.'='.urlencode($value).'&'; }
        rtrim($post_string, '&');
        curl_setopt($ch,CURLOPT_POST, count($post));
        curl_setopt($ch,CURLOPT_POSTFIELDS, $post_string);
    }
    $res = curl_exec($ch);
    if (curl_errno($ch)) {
        $this->deliver_responce('201','Couldn\'t send request: ' . curl_error($ch));exit();
    }
    else {
        $resultStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        if ($resultStatus == 200) {
            //echo "Post Successfully!";
            return true;
        }
        else{
            $this->deliver_responce('200','Request failed: HTTP status code: ' . $resultStatus);exit();
        }
    }
}

I have to call it with URL and array that I want to post. It's working when the array has key and value but not working when I have nested array. It’s working for:

array(
    'id'=>1,
    'name'=>'apple'
)

but not working for

array(
    'id'=>5,
    'cmd'=>array('password','encrypt','decrypt')
)

I think problem is at:

foreach($post as $key=>$value) { $post_string .= $key.'='.urlencode($value).'&'; }

In my function but I don't know how to do it.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • 已采纳
    douci1196 douci1196 2019-03-29 10:49

    it's solved changing foreach loop like

    foreach($post as $key=>$value) { 
        if (is_array($value)) {
            foreach ($value as $val) {
                $post_string .= $key.'[]='.urlencode($val).'&';
            }
        } 
        else{ 
            $post_string .= $key.'='.urlencode($value).'&'; 
        }
    }
    
    点赞 评论 复制链接分享
  • doufendi9063 doufendi9063 2019-03-28 07:42

    To create a request uri there's a http_build_query function:

    if (!empty($post)) {
        curl_setopt($ch,CURLOPT_POST, true);
        curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query($post));
    }
    

    However, according to manual (emphasized by me):

    This parameter can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value.

    So, you can try to pass $post array directly, without transforming it to string.

    点赞 评论 复制链接分享
  • duan0708676887 duan0708676887 2019-03-28 07:49

    For this kind of post data you should use JSON data , and parse json on server side. But if you dont have control over url to which you are sending data you can send nested array using following lines. no need to convert array inyo string manuallly.

    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: multipart/form-data"));
    
    curl_setopt($ch, CURLOPT_POSTFIELDS, urldecode(http_build_query($post_array)));
    
    点赞 评论 复制链接分享

相关推荐