Occurrence Download API in PHP

Hi,

I’m really new to this whole topic. Never worked with cURL before. So please be patient.

This is my actual code:

    $data = array(
	'creator' => 'username',
	'notificationAddresses' => ["myown@email.com"],
	'sendNotification' => true,
	'format' => 'SIMPLE_CSV',
	'predicate' => array(
		'type' => 'and',
		'predicates' => array(
			array(
				'type' => 'equals',
				'key'	=> 'TAXON_KEY',
				'value' => '5219683'
			),
			array(
				'type'	=> 'equals',
				'key' => 'HAS_COORDINATE',
				'value'=> true
			)
		
		)
		
	)
);                                                                    
 $data_string = json_encode($data);    
                                                                                                                     
$ch = curl_init('https://api.gbif.org/v1/occurrence/download/request/');                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_USERPWD, 'username:password');                                                                     
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json'
)                                                                       
);                                                                                                                   
                                                                                                                 
$result = curl_exec($ch);
var_dump($result);
curl_close($ch);

This is my JSON:

{"creator":"username","notificationAddresses":["my@email.com"],"sendNotification":true,"format":"SIMPLE_CSV","predicate":{"type":"and","predicates":[{"type":"equals","key":"TAXON_KEY","value":"5219683"},{"type":"equals","key":"HAS_COORDINATE","value":true}]}}

But nothing happens. So appenrently I’m doing something wrong here.
Please point me in the right direction.
Thanks.

1 Like

Thanks @eutervogel

I’m really new to this whole topic. Never worked with cURL before. So please be patient.

Welcome to the community. I don’t know PHP myself, but we’ll work out how to get you running.

Your code snippet creates the JSON but you then need to POST it to the download service. I think you might have left that part out of your example. Can you please provide the complete example somewhere (here or on github or a gist or so) so we can help diagnose it? [Apologies, on my browser that code block doesn’t indicate it is scrollable. I will try and explore this PHP]

It would also be useful for us to hear why you are using PHP for this. Are you developing a web application perhaps? There is certainly nothing wrong with using PHP and I am just curious as Python and R are the most common scripting languages we see in our user community.

2 Likes

Hi @eutervogel

This PHP code (which is your code, but also showing the HTTP response) works for me:

    $data = array(
            'creator' => 'username',
            'notificationAddresses' => ["myown@email.com"],
            'sendNotification' => true,
            'format' => 'SIMPLE_CSV',
            'predicate' => array(
                    'type' => 'and',
                    'predicates' => array(
                            array(
                                    'type' => 'equals',
                                    'key'=> 'TAXON_KEY',
                                    'value' => '5219683'
                            ),
                            array(
                                    'type'=> 'equals',
                                    'key' => 'HAS_COORDINATE',
                                    'value'=> true
                            )
                    )
            )
    );
    $data_string = json_encode($data);

    $ch = curl_init('https://api.gbif.org/v1/occurrence/download/request');

    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_USERPWD, 'username:password');
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

    $response = curl_exec($ch);

    $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
    $header = substr($response, 0, $header_size);
    $body = substr($response, $header_size);

    curl_close($ch);

    // Headers
    print($header);

    // Download id (if successful)
    print($body);

Could you try that, and check if your authentication was correct? If your password is wrong, you’ll get “HTTP/1.1 401 Unauthorized”. You should get “HTTP/1.1 201 Created” and the download key.

1 Like

@eutervogel There is certainly nothing wrong with using PHP and I am just curious as Python and R are the most common scripting languages we see in our user community. You should keep on this project. :slightly_smiling_face:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.