PHP and API Integration

#1

I am using the tutorial to “create a new deal” I have correctly entered the API Token,organization ID and company domain. Every time I run the script “create_deal.php” the request is sent but I do not get a response and the deal isn’t posted. Any help would be appreciated!!
I am using PHP 7.3.5, and below is the code:

 <?php
// Content of create_deal.php
 
// Pipedrive API token
$api_token = 'xxxxx';
 
// Deal title and Organization ID
$deal = array(
  'title' => 'New Deal Test',
  'org_id' => '1'
);
 
$url = 'https://<company-domain>.pipedrive.com/v1/deals?api_token=' . $api_token;
 
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $deal);
 
echo 'Sending request...' . PHP_EOL;
 
$output = curl_exec($ch);
curl_close($ch);
 
// Create an array from the data that is sent back from the API
// As the original content from server is in JSON format, you need to convert it to PHP array
$result = json_decode($output, true);
 
// Check if an ID came back, if did print it out
if (!empty($result['data']['id'])) {
   echo 'Deal was added successfully!' . PHP_EOL;
}
#2

Hi,
Is curl set up correctly on your machine/server?

P.S. (Please change your API token, and avoid posting it in the future for security reasons :wink: )

#3

I have verfied that Curl is working correctly, I curled Google and received a response. Still not able to post new deals? API token changed too.

#4

Try changing this

curl_setopt($ch, CURLOPT_POSTFIELDS, $deal);

to

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($deal));

:crossed_fingers:

#5

Dani,

I corrected the line to “curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($deal));” and still no response from the API.

#6

What do you get if you just echo the whole $output? (After calling curl_exec)

echo $output;
#7

No response after the echo.
image

#8

And, just to make sure, this is after curl_exec?

Have you already tried debugging line by line?

#9

What about the TLS version? Are you using TLS 1.2?
https://pipedrive.readme.io/docs/changelog#section-05-12-18-deprecation-of-transport-layer-security-tls-1-0-and-1-1-encryption-protocols

#10

I debugged PHP and even turned on error reporting just to be sure, no issues. I just added “curl_setopt($ch, CURLOPT_SSLVERSION, 6);” to force TLS 1.2. still getting no response.

Just asking has anyone gotten the sample code to work from the tutorial?

#11

Yes, it’s a very simple script… I also just re-tested it just to make sure.

I can’t think of much else in this moment, honestly. You could try to:

  • Make the same (similar) POST request to another API to isolate the problem (I suspect it’s something to do with CURL on your machine)
  • Check if any request is actually firing at all
#12

Dani,

I got it! I added not to verify the peer “curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);” and the deal added!!! :slight_smile:
image

Thanks for your help

1 Like