How to make a PUT request in PHP for CRM API integration

I’m having trouble with making PUT requests using PHP and cURL. I can successfully handle GET and POST requests to a CRM API, but I’m stuck when it comes to updating existing records.

I’m trying to link an opportunity with a customer record in the CRM system. The API documentation mentions that I need to use a PUT request to update existing records and create associations between them.

When I try to access the API endpoint directly in my browser, I get a 405 Method Not Allowed error. This makes sense since browsers typically send GET requests by default.

I attempted to use cURL in PHP but I’m confused about the proper setup. Since the association data seems to be included in the URL endpoint itself, I’m not sure what data I should be sending in the request body.

Here’s what I’m trying to achieve:

$opportunityId = '12345';
$customerId = '67890';
$apiKey = 'my_api_key';

$url = 'https://api.example.com/opportunities/v1/opportunity/' . $opportunityId . '/associations/CUSTOMER?id=' . $customerId . '&key=' . $apiKey;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
// What should I put here for the data?
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);

Can someone help me understand what I’m missing with PUT requests when the association data is in the URL?

Your cURL setup looks good, but you’re missing a few headers that’ll make it work. Since your association parameters are already in the URL, you don’t need anything in the request body for this endpoint. I’ve dealt with similar CRM APIs before - you need to add CURLOPT_HTTPHEADER with Content-Type: application/json and Content-Length: 0. Also throw in CURLOPT_POSTFIELDS with an empty string. Some APIs expect this even when you’re not sending body data with PUT requests. The 405 error in your browser is totally normal like you said. Double-check that your API key has permissions for updating associations. I’d add some error handling after curl_exec() to check the HTTP response code - helps catch auth or permission problems.

I hit this same issue with HubSpot’s API last year. You’re probably missing SSL verification settings. Try adding CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST set to false for testing (just remember to enable them in production). Also, tons of CRM APIs will flat-out reject requests without a User-Agent header - definitely worth adding. For PUT requests with URL params like yours, you don’t usually need body data, but the server might still want specific headers to validate properly. Set CURLOPT_VERBOSE to true temporarily and you’ll see exactly what’s getting sent and received. This’ll show you any redirects or missing headers that aren’t mentioned in their docs.