简体   繁体   English

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

[英]How to pass array in curl with &?

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. 我必须使用我要发布的URL和数组来调用它。 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. 在我的功能,但我不知道该怎么做。

To create a request uri there's a http_build_query function: 要创建请求uri,有一个http_build_query函数:

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. 此参数可以作为urlencoded 字符串传递,如'para1 = val1&para2 = val2&...',也可以作为数组传递,字段名称为键,字段数据为值。

So, you can try to pass $post array directly, without transforming it to string. 因此,您可以尝试直接传递$post数组,而无需将其转换为字符串。

For this kind of post data you should use JSON data , and parse json on server side. 对于这种发布数据,您应该使用JSON数据,并在服务器端解析json。 But if you dont have control over url to which you are sending data you can send nested array using following lines. 但是,如果您无法控制要发送数据的URL,则可以使用以下行发送嵌套数组。 no need to convert array inyo string manuallly. 无需手动转换数组inyo字符串。

curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: multipart/form-data"));

curl_setopt($ch, CURLOPT_POSTFIELDS, urldecode(http_build_query($post_array)));

it's solved changing foreach loop like 它解决了改变foreach循环的问题

foreach($post as $key=>$value) { 
    if (is_array($value)) {
        foreach ($value as $val) {
            $post_string .= $key.'[]='.urlencode($val).'&';
        }
    } 
    else{ 
        $post_string .= $key.'='.urlencode($value).'&'; 
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM