Reputation: 3506
I want to know if I post my JSON successfully to this url http://localhost/api_v2/url/post?key=***
, so I can retrieve them back eventually, but I am not sure, how I would test them.
Normally, we can just print_r($result )
to see what is in the variable, but when I did that nothing showed up.
When I do echo $result
also nothing showed up.
So far, nothing help, so I decide to move up an next variable $ch
when I do echo $ch;
I got this Resource id #188
. Now, I am stuck.
Can someone help me clarify on this ?
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!";
}
}
Here is my routes
// 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'));
});
Upvotes: 0
Views: 1592
Reputation: 153030
I suggest you use the returned HTTP status code to determine if the request was successful:
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if($status == 200){
echo "Post Successfully!";
}
I also think something isn't working quite right since the dd($result)
shows a 404 page. Probably there's no route matching POST http://localhost/api_v2/url?key=***
I'm not the curl expert but as far as I know you should pass that data as an array:
curl_setopt($ch, CURLOPT_POSTFIELDS, array('data' => $json));
On the other end you can retrieve it like that:
$data = json_decode(Input::get('data'));
To use the CSRF filter on a single route:
Route::post('url/post', array('before' => 'csrf|api_v2', 'uses' => 'UrlController@post'));
Or alternatively using CSRF for a certain HTTP verb (and prefix if you want)
Route::when('*', 'csrf', array('post'));
Upvotes: 2