I use CartoDB import api to upload csv file correctly, and I can see the 'pointtest' table is created in the cartoDB website. By following command line:
#curl -v -F file=@/mnt/hgfs/D/cartodb/pointtest.csv "https://{username}.cartodb.com/api/v1/imports/?api_key={apikey}"
but when I use php to rewrite this above command line, there are nothing happened,
upload.php
<?php
echo "Hello World";
$url = "https://{username}.cartodb.com/api/v1/imports/?api_key={apikey}";
$file = getcwd()."/pointtest.csv";//locate in /var/www/html/pointtest.csv
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => array(
CURLOPT_SSL_VERIFYPEER => false,
userfile => '@'.$file
)
));
$result = curl_exec($ch);
curl_close($ch);
echo $result;?>
I get a success message
{"item_queue_id":"fcbfd6ag-c8df-47b2-8a16-fa8392576f2c","success":true}
But there is nothing happened on my cartodb web page.
Than I try to use this "item_queue_id" append to my $url and curl_exec(),
there is nothing happened. So I try to test four case as following:
A as I use command line to get the item_queue_id.
B as I use php code to get the item_queue_id.
C as I use command line to upload my "testpoint.csv" data.
D as I use php code to upload my "testpoint.csv" data.
First, A than C, it works well.
Second, A than D, it works well.
Third, B than C, it gets error message as following:
* Connection #0 to host {username}.cartodb.com left intact{"id":"XXX-xXXX-XXXX-XXXX-XXXXXXXXXXXX","user_id":"XXXXXXXX","table_id":null,"data_type":"datasource","table_name":null,"state":"failure","error_code":99999,"queue_id":"aa175163-ec18-4f8a-99eb-d5683264ac0a","tables_created_count":null,"synchronization_id":null,"type_guessing":true,"quoted_fields_guessing":true,"content_guessing":false,"create_visualization":false,"visualization_id":null,"user_defined_limits":"{\"twitter_credits_limit\":0}","get_error_text":{"title":"Unknown","what_about":"Sorry, something went wrong and we're not sure what. Try\n uploading your file again, or <a href='mailto:[email protected]?subject=Unknown error'>contact us</a> and we'll try to help you quickly.","source":"cartodb"},"display_name":"XXX","success":false,"original_url":"","warnings":null}
Fourth, B than D, it shows nothing on my chrome.
I don't know how to deal with it.
-
Did you perform any debugging yourself? Check logfiles from the server, your browser console ([f12]-key), etceteraStefan– Stefan2016年01月08日 09:19:32 +00:00Commented Jan 8, 2016 at 9:19
-
@Stefan Thank's for your reply. I rewrite my code and get a successful message, but there are nothing happen on my cartodb web page.steven– steven2016年01月11日 09:24:25 +00:00Commented Jan 11, 2016 at 9:24
2 Answers 2
Thank's for iriberri and CartoDB support, The answer is to replace userfile with 'file'.
$API_KEY = "xxxx"; $url = "https://xxx.cartodb.com/api/v1/imports/"; $file = getcwd()."/xxx.csv"; $ch = curl_init(); curl_setopt_array($ch, array( CURLOPT_RETURNTRANSFER => true, CURLOPT_URL => $url, CURLOPT_POST => true, CURLOPT_POSTFIELDS => array( api_key => $API_KEY, CURLOPT_SSL_VERIFYPEER => true, 'file' => '@'.$file ) )); $result = json_decode(curl_exec($ch),true); if( ! $result) { echo "Error"; } curl_close($ch);
With your Import ID, you should be able to run an authenticated request to the status endpoint to see what's the status of the upload.
The endpoint is:
GET /api/v1/imports/<import_id>
In your case it would be:
https://username.cartodb.com/api/v1/imports/fcbfd6ag-c8df-47b2-8a16-fa8392576f2c?api_key=your_api_key
More info on the official docs: http://docs.cartodb.com/cartodb-platform/import-api/standard-tables/#check-the-status-of-an-import-process
-
Thank's for your reply. I rewrote my code and did some test, but I still couldn't use php code to process uploaded.steven– steven2016年01月12日 07:42:55 +00:00Commented Jan 12, 2016 at 7:42
-
Your new info seems to be showing ""success":false" and "state":"failure". If you send your error ID ("id" in that response that is hidden) to [email protected] they could check for you what failed for your file. You can also try to upload the same file to the CartoDB Editor, if it works in there, then you could discard that there's an issue with the file itself.iriberri– iriberri2016年01月12日 16:03:06 +00:00Commented Jan 12, 2016 at 16:03
-
Thank's a lot. I could use CartoDB Editor to upload the same file and it works well. I will email my question to [email protected] asking for help, thank you again for your help.steven– steven2016年01月13日 01:44:57 +00:00Commented Jan 13, 2016 at 1:44