【发布时间】:2015-03-30 09:12:43
【问题描述】:
目标
我想知道我是否成功地将我的 JSON 发布到这个 url http://localhost/api_v2/url/post?key=***,所以我最终可以取回它们,但我不确定如何测试它们。
我试过了
通常,我们可以通过print_r($result ) 来查看变量中的内容,但是当我这样做时,什么都没有显示。
当我执行echo $result 时,也没有任何显示。
到目前为止,没有任何帮助,所以我决定在执行 echo $ch; 时将下一个变量 $ch 向上移动,我得到了这个 Resource id #188。现在,我被困住了。
有人可以帮我澄清一下吗?
这是我的代码
public function post(){
$ch = curl_init("http://localhost/api_v2/url?key=***");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$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);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($json))
);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('data' => $json));
$result = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if($status == 200){
echo "Post Successfully!";
}
}
(更新)
这是我的路线
// API Version 2
Route::group(array('prefix' => 'api_v2'), function(){
Route::post('url/post', array('before' => 'api_v2', 'uses' => 'UrlController@post'));
Route::get('url/reveive', array('before' => 'api_v2', 'uses' => 'UrlController@receive'));
Route::get('url/store', array('before' => 'api_v2', 'uses' => 'UrlController@store'));
});
【问题讨论】:
标签: php json laravel curl laravel-4