在CURL中传递数组以及php中的字段

时间:2016-05-18 09:13:10

标签: php curl

我想将数组与CURL中的字段一起传递 这就是我想要做的事情,

$url = Configurations::DOMAIN . '/register';
$fields = "username=software3@gmail.com&password=test123&hobbies=array('cricket','football')";

    $ch = curl_init();
    //set the url, number of POST vars, POST data
    curl_setopt($ch, CURLOPT_URL, $url);

    curl_setopt($ch, CURLOPT_HEADER, 1);

    // receive server response ...
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    if (!is_null($fields)) {
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
        curl_setopt($ch, CURLOPT_USERAGENT, Configurations::USER_AGENT);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

        //Set Cookie
        curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIE_FILE);
        curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIE_FILE);
    } else {
        curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIE_FILE);
    }
    //execute post
    $result = curl_exec($ch);

我想要类似的东西,

["hobbies"]=>
array(1) {
  [0]=>
  string(1) "cricket",
  [1]=>
  string(1) "football"
}

但是,获得,

["hobbies"]=>
string(10) "array('cricket','football')"

写$ field的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

在某些搜索的帮助下,解决方案是使用,

..&hobbies[]=cricket&hobbis[]=football..

这是正确的方法。