PHP(CI)cURL传递的多维数组不像一个行为(无法循环) [英] PHP (CI) cURL passed multidimensional array does not behave as one (Can't loop it)

查看:194
本文介绍了PHP(CI)cURL传递的多维数组不像一个行为(无法循环)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从POST cURL请求接收参数时,我遇到了问题。不管我怎么编码它(json,url,rawurl,utf8,base64 ...)之前POST它,我不能通过数组元素,通过循环执行任何解码操作。我给你的细节。

I'm having an extrange issue when receiving parameters from a POST cURL request. No matter how I encode it (json, url, rawurl, utf8, base64...) before POSTing it, I am not able to perform any decoding operation through the array elements, via loop. I'm giving you the details.

从消费控制器,在其他一些php(Yii)应用程序,我建立我的请求,如下:

From the consuming controller, in some other php (Yii) app, I build my request like this:

private function callTheApi($options)
{
    $url = "http://api.call.com/url/api";

    $params = array(    'api_key' => $this->api_key,
                        'domain' => $this->domain,
                        'date' => $options['date'],
                        'keys' => $options['keys'] // This is an array
    );

    // Following some good advice from Daniel Vandersluis here:
    // http://stackoverflow.com/questions/3772096/posting-multidimensional-array-with-php-and-curl
    if (is_array($params['keys'])
    {
        foreach ($params['keys'] as $id => $name)
        {
            $params['keys[' . $id . ']'] = $name;
        }
        unset($params['keys']);
    }

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: multipart/form-data; charset=utf-8'));
    curl_setopt($ch, CURLOPT_TIMEOUT, 120);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
    curl_setopt($ch, CURLOPT_USERAGENT, 
            "Mozilla/5.0 (Windows; U; Windows NT 5.0; en; rv:1.9.0.4) "
            . "Gecko/2009011913 Firefox/3.0.6");

    $output = curl_exec($ch);

    $error = curl_errno($ch);
    $error_text = curl_error($ch);

    curl_close($ch);

    if (!$output || $error != 0)
        die("<br><hr>Problems...<br>"
            . "Line:" . __LINE__ . " dataExtractor.php<br>"
            . "Error: " . $error . " - " . $error_text . "<hr>" 
            . $url . "<hr>");

    sleep(1);

    return json_decode($output, true);
}

在api本身,这是函数:

And in the api itself, this is the function:

public function api()
{
    $params = $_POST;

    foreach($params as $k=>$v){
        if($k=='domain') $domain = $v;
        if($k=='date') $date = $v;
        if($k=='api_key') $api_key = $v;
        if($k=='keys') $keys = $v;
    }

    echo json_encode($keys);
    // All my logic would be here, after parsing the array correctly.
}

好,现在问题:

如果我把一切都像以前说的,它的工作原理。我有我的$ keys数组在api,我可以使用它,但我想要的。 echo json_encode($ keys)句子返回数组ALMOST,因为它应该是。但问题是数组的一些值在cURL操作中被损坏。诸如西班牙字符á,é,í,ó,úORü的值根本不存在于array_values中。

If i leave everything like stated before, it works. I have my $keys array in the api, and I can use it however I want. The "echo json_encode($keys)" sentence returns the array ALMOST as it should be. But the problem is some values of the array are corrupted in the cURL operation. Values such as spanish characters á, é ,í, ó, ú OR ü are simply not present in the array_values.

如果$ keys数组中的某些键在原始数组中是西班牙单词alimentación,一旦它被cURL到api,它变成alimentacin。那里,ó不再有了。

If some key in the $keys array was spanish word "alimentación" in the original array, once it's been cURLed to the api, it becomes "alimentacin". There, the ó is not there anymore.

所以,我的机会是将数组中的每个值编码为一个安全传送的值,以便我以后可以解码。但你知道,我不能。

So, my chances are encoding each value in the array to a safely transferred value, so that I can decode it later. But what do you know, I can't.

我试过urlencoding,rawurlencoding,json_encoding,base64_encoding ...数组的每个值。如果我从api返回接收的数组,它包含编码的值。但。

I've tried urlencoding, rawurlencoding, json_encoding, base64_encoding... each value of the array. And if I return the received array from the api, it contains the encoded values all right. BUT.

如果我在api中循环数组进行解码,然后尝试返回它,无论我对它的值应用什么解码函数,输出都是ALWAYS 空值。

If I loop the array in the api for decoding, and then try to return it, no matter what decoding function I'm applying to its values, the output is ALWAYS "NULL".

我不知道我在这里做错了什么。差远了。
所以任何帮助将非常感激。提前感谢社区。

I have no clue what I'm doing wrong here. Not even close. So any help would be much appreciated. Thanks in advance, community.

推荐答案

创建cUrl params数组时,应该知道键不能是utf8。

When you create cUrl params array you should know that keys cannot be utf8.

当您在foreach循环中添加一些参数

And when you add some parameters in foreach loop

$params['keys[' . $id . ']'] = $name;

$ id可以是utf8字符。

$id can be utf8 character.

为了避免我建议您使用json_encode

To avoid that I recommend you to use json_encode

$params = array(    
    'api_key' => $this->api_key,
    'domain' => $this->domain,
    'date' => $options['date'],
    'keys' => json_encode($options['keys']) // This is an array
);

在您的api中,在这种情况下,您什么也不应该更改。

In your api in this case you should change nothing.

这篇关于PHP(CI)cURL传递的多维数组不像一个行为(无法循环)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆