简体   繁体   中英

MapBox - Unable to create new empty dataset with api and php cUrl. I get message not found error

I have the following php cUrl script. I am trying to create a new empty dataset on mapbox via their api. The below receives a form submission of dh_dataset_name and dh_dataset_description then then encodes it to json and then submits to the api with cUrl, however, I get an error of {"message":"Not Found"} . How can I submit to mapbox's api to create an empty dataset using cUrl php? Any pointers appreciated.

        $ski_datasets_create = $this->request->getPost();
       
        $data[] = array(
            'name' => $this->request->getPost('dh_dataset_name'),
            'description' => $this->request->getPost('dh_dataset_description')
        );
       
        $payload = json_encode($data);
       
        $url = "https://api.mapbox.com/datasets/v1/[snip:username]?access_token=[snip]";
       
        $request = curl_init();
        curl_setopt($request, CURLOPT_URL,            $url);
        curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($request, CURLOPT_CUSTOMREQUEST,  "POST");
        curl_setopt($request, CURLOPT_POSTFIELDS,     $payload ); 
        curl_setopt($request, CURLOPT_HTTPHEADER,     array('Content-Type: application/json'));

        $repsonse = curl_exec($request);
        
        echo '<pre>';
        curl_errno($request);
        curl_error($request);   
        print_r($repsonse);    
        echo '</pre>';
        die();

I only get below error message which is not helpful: {"message":"Not Found"}

There is actually a very cool feature on POSTMAN. If you are able to get it working in POSTMAN, there is an export code feature. The below is a modified version of the php cURL export but it works:

        $ski_datasets_create = $this->request->getPost();
       
        $data = array(
            'name' => $this->request->getPost('dh_dataset_name'),
            'description' => $this->request->getPost('dh_dataset_description')
        );
       
        $payload = json_encode($data);

        $url =  $this->data['dh_mapbox_api_url'].$this->data['dh_mapbox_username'].'?access_token='.$this->data['dh_mapbox_admin_token']; 
        
        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => $payload,
            CURLOPT_HTTPHEADER => array(
                'Content-Type: application/json'
            ),
        ));
        
        $response = curl_exec($curl);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM