Php 如何修复卷曲柱';发布数据?
处境Php 如何修复卷曲柱';发布数据?,php,arrays,json,curl,laravel-4,Php,Arrays,Json,Curl,Laravel 4,处境 我有一个PHP脚本,它假设从我的Windows7机器向我的服务器发送POST请求 现在我有两件事: 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脚本时,我得到了0 这意味着
- 我有一个PHP脚本,它假设从我的Windows7机器向我的服务器发送POST请求
- 现在我有两件事:
1- Standalone PHP script running ( on a Windows 7 machine ) 2- A function that receive data from what I post ( On my server )
- 最后,当我运行我的PHP脚本时,我得到了
0
- 这意味着我从
returncount($json)返回了响应代码>,但响应错误:(
- 我的$json变量中似乎没有任何内容
我就是搞不懂-到底是什么阻止我的cURL将数据发布到我的服务器,我想知道是否有人能帮我解释这一切。删除这两行:
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,array('json'=>$json));
添加这一行:
curl\u setopt($ch,CURLOPT\u POSTFIELDS,json\u encode(数组('json'=>$json));
100%工作!
感谢
@KimAlexander
的建议。您是否检查了原始post数据以确保您的服务器脚本正在接收发布的数据?基本调试:不要链接调用。首先查看Input::get(…)
返回的内容。然后尝试对其进行解码,var_dump()解码后的文本,等等。尝试以下方法:curl\u setopt($ch,CURLOPT_POSTFIELDS,$json);
或者如果您需要您的变体:curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode(array('json'=>$json));
谢谢大家的建议。真的有用吗?
<?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;
?
public function post() {
$json = json_decode(Input::get('json'));
return count($json);
}