我想知道我是否将JSON成功发布到此网址http://localhost/api_v2/url/post?key=***
,因此我最终可以将其恢复,但我不确定,我将如何测试它们。
通常情况下,我们可以print_r($result )
查看变量中的内容,但是当我这样做时,没有任何内容显示出来。
当我echo $result
时,也没有任何表现。
到目前为止,没有任何帮助,所以当我做$ch
时,我决定提升下一个变量echo $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'));
});
答案 0 :(得分:2)
我建议您使用返回的HTTP状态代码来确定请求是否成功:
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if($status == 200){
echo "Post Successfully!";
}
由于dd($result)
显示404页面,我也认为某些内容无法正常工作。可能没有匹配POST http://localhost/api_v2/url?key=***
我不是卷曲专家,但就我所知,你应该将数据作为数组传递:
curl_setopt($ch, CURLOPT_POSTFIELDS, array('data' => $json));
另一方面,您可以像这样检索它:
$data = json_decode(Input::get('data'));
在单条路线上使用CSRF过滤器:
Route::post('url/post', array('before' => 'csrf|api_v2', 'uses' => 'UrlController@post'));
或者将CSRF用于某个HTTP动词(如果需要,还可以使用前缀)
Route::when('*', 'csrf', array('post'));