About the API category

#1

Our open API allows you to create integrations and apps that will work directly with Pipedrive. Best place for topics relating to API

#2

Sometimes I am facing an issue in creating an organisation, person and deal. This issue is not occurring every time, but it happens sometimes.

I am using php curl for creating organisation, person and deal. Below is my code snippet. This code is called using Ajax request with post data.

Please advise if I am going wrong with this below code.

<?php
use \Curl\Curl;
if(isset($_POST)){
    $post = $_POST; //all post data in a variable
    $curl = new Curl();
    $curl->setHeader('Content-Type', 'application/json');
    $token = TOKEN; // pipedrive token
    // Add Organization
    $customer_name = $_POST['customerfname']." ".$_POST['customerlname'];
    $url = "https://api.pipedrive.com/v1/organizations?api_token=".$token;   
    $org = ['name' => 'Company of ' . $customer_name];
    $curl->post($url, $org);
    $result1 = $curl->response;
    if($result1->success == true) {
        $org_id = $result1->data->id;
        //Create a person
        $persons = ['name' => $customer_name, 'email' => $post['Email'], 'phone'=> $post['phone'], 'org_id' => $org_id];
        // Add Person
        $url = "https://api.pipedrive.com/v1/persons?api_token=".$token;
        $curl->post($url, $persons);   
        $result = $curl->response;           
        if($result->success == true){
            $person_id = $result->data->id;
            // Add Deal
            $url = "https://api.pipedrive.com/v1/deals?api_token=".$token;
            $deal = [
                'title' => $customer_name,       
                'org_id' => $org_id,
                'person_id' => $person_id,
                "60622b5221f5668fa3399c15gh521556fgh13554" => (isset($post['Description']))?$post['Description']:"",
                "15cc7af921f5668fa3399c15gh521556f3f823091" => (isset($post['message']))?$post['message']:"",
                "stage_id"=>30
            ];
            $curl->post($url, $deal);
            $result2 = $curl->response;           
            if($result2->success == true){       
                   //file upload process
                   if(count($_POST['allfiles'])==0){ //check file available or not
                        echo 1;
                    }
                    else {
                        $deal_id = $result2->data->id;                                           
                        // Add Files
                        $url = "https://api.pipedrive.com/v1/files?api_token=".$token;           
                                                $c = count($_POST['allfiles']);   
                        for($i = 0;$i<$c;$i++){
                            $curl->setHeader('Content-Type', 'multipart/form-data');
                            $file = ['deal_id' => $deal_id,       
                            'person_id' => $person_id,
                            'file'=>new CURLFile(dirname(__FILE__) . '/uploads/doc/'.$_POST['allfiles'][$i])
                            ];                                   
                            $curl->post($url, $file);
                            $result3 = $curl->response;
                                                        $path = "uploads/doc/".$_POST['allfiles'][$i];
                                                        unlink($path);
                        }
                        if($result3->success == true){           
                            echo 1;
                        }
                        else{
                            echo 0;
                        }   
                    }                               
            }
            else{ echo 0; }
        }
        else{ echo 0; }
    }
    else{ echo 0; }
}
else{ echo 0; }
?>