情况
所以现在我有两件事:
1- Standalone PHP script running ( on a Windows 7 machine )
2- A function that receive data from what I post ( On my server )
这是他们的样子:
1-运行独立PHP脚本(在Windows 7计算机上)
<?php
// Load and Convert : csv > UTF8 > array > JSON
$file_name = 'inventory.csv';
$file_path = 'C:\\QuickBooks\\'.$file_name;
$csv= file_get_contents($file_path);
$utf8_csv = utf8_encode($csv);
$array = array_map("str_getcsv", explode("\n", $utf8_csv));
$json = json_encode($array, JSON_PRETTY_PRINT);
//echo $json; // It's working !
$ch = curl_init("http://localhost/api_inventory/url?key=*****");
// Set cURL options
curl_setopt($ch, CURLOPT_USERPWD, "admin:*****");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('json' => $json));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute with clean exit
$response = curl_exec($ch);
curl_close($ch);
echo $response;
?
2-从我发布的内容(在我的服务器上)接收数据的功能
public function post() {
$json = json_decode(Input::get('json'));
return count($json);
}
0
。return count($json);
收到了回复,但回复错误。 :(我无法弄清楚 - 将{cURL从Posting
数据停止到我的服务器究竟是什么原因,我想知道是否有人可以帮我解释所有这些。< / p>
答案 0 :(得分:1)
删除这2行:
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('json' => $json));
添加这一行:
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array('json' => $json)));
感谢@KimAlexander
建议。