如何确定使用 PHP cURL 发布 JSON 是否成功?
How to determine if posting JSON with PHP cURL is succeed?
目标
我想知道我 post 我的 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'));
});
建议您使用返回的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 专家,但据我所知,您应该将该数据作为数组传递:
curl_setopt($ch, CURLOPT_POSTFIELDS, array('data' => $json));
在另一端,您可以像这样检索它:
$data = json_decode(Input::get('data'));
编辑 2
要在单个路由上使用 CSRF 过滤器:
Route::post('url/post', array('before' => 'csrf|api_v2', 'uses' => 'UrlController@post'));
或者对某个 HTTP 谓词使用 CSRF(如果需要,还可以加上前缀)
Route::when('*', 'csrf', array('post'));
目标
我想知道我 post 我的 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'));
});
建议您使用返回的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 专家,但据我所知,您应该将该数据作为数组传递:
curl_setopt($ch, CURLOPT_POSTFIELDS, array('data' => $json));
在另一端,您可以像这样检索它:
$data = json_decode(Input::get('data'));
编辑 2
要在单个路由上使用 CSRF 过滤器:
Route::post('url/post', array('before' => 'csrf|api_v2', 'uses' => 'UrlController@post'));
或者对某个 HTTP 谓词使用 CSRF(如果需要,还可以加上前缀)
Route::when('*', 'csrf', array('post'));