通过Curl发送JSON数据时出错 - “json文本中的无效字符”

时间:2014-12-10 18:57:52

标签: php json curl basecamp

我正在尝试创建一个项目来试验Basecamp API。

我正在使用以下代码:

    #CONTENT-TYPE HEADER
$helloHeader = "User-Agent: $appName ($appContact)";

#URL
$url= $baseUrl.'/projects.json';

#JSON DATA
$data = array
(
    'nome' => 'project xpto'
);

$data_aux = json_encode($data);

$data_string = 'json=' . $data_aux . '&';

#INITIALIZE
$ch = curl_init($url);

#standard
curl_setopt($ch, CURLOPT_USERPWD, $credentials);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);

#OBTAIN DATA
//curl_setopt($ch, CURLOPT_HTTPGET, true);

#SEND DATA
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Content-Type: application/json",
    "User-Agent: " . $appName . "(" . $appContact . ")")
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);

$response = curl_exec($ch);

#ERRORS?
$errno = curl_errno($ch);
$error = curl_error($ch);

#CLOSE
curl_close($ch);

我遇到以下错误:

lexical error: invalid char in json text.
                                   json={"nome":"project xpto"}&
                 (right here) ------^

知道我做错了什么吗?我试过通过几种方式更改数组但我无法理解这个错误!

0 个答案:

没有答案