0

I'm trying to make a post request to salesforce "api". however it accepts only content type which is explicitly set to "application/x-www-form-urlencoded"

When I do this:

curl_setopt_array($ch, array(
                CURLOPT_URL => 'https://www.salesforce.com/servlet/servlet.WebToLead',
                CURLOPT_RETURNTRANSFER => 1,
                CURLOPT_POST => 3,
                CURLOPT_POSTFIELDS => json_encode(array (
                        'first_name' => 'foo',
                        'last_name' => 'faa',
                        'email' => '[email protected]',
                        'oid' => '#hash',
                        'recordType' => '#hash'
                )),
                CURLOPT_HTTPHEADER=>array(
                        'Content-type: application/x-www-form-urlencoded'
                )
        ));

        $data = curl_exec($ch);
        $info = curl_getinfo($ch);

The response headers content-type is always: "text/html;charset=UTF-8"

The same parameters I send using postman (with the correct header) actually works.

2
  • So the problem is that you're not getting any response from the salesforce api using the code above? Are you getting an error response? Can you provide a link to the salesforce docs that you're using? Commented Sep 30, 2014 at 12:41
  • nope. It is a horrible API that doesn't provide any feedback. I know if my request succeeded by going to salesforce -> leads, and check if the lead actually created. I'm pretty sure the problem is that Content-type header. With postman I can post successfully to salesforce. When I change the content-type on postman it also fails. Commented Sep 30, 2014 at 12:51

1 Answer 1

1

When CURLOPT_POST is true, curl sets Content-Type of the request to application/x-www-form-urlencoded automatically; you don't need to do that manually. You can verify that (as well as check all other "outgoing" headers) by setting CURLINFO_HEADER_OUT to true prior to your request, then checking the array returned by curl_getinfo().

I suspect your problem has nothing to do with the request headers but with how curl handles https. Setting these in your curl_setopt_array() should help:

CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => 2,
Sign up to request clarification or add additional context in comments.

3 Comments

This is my request after the change: curl_setopt_array($ch, array( CURLOPT_URL => 'salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8', CURLOPT_POST=> true, CURLOPT_RETURNTRANSFER=> true, CURLOPT_POSTFIELDS => json_encode($fields), CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => 2 )); Where $fields is an array. But still the output from curl_getinfo clearly shows: content_type: "text/html;charset=UTF-8"
You are right about the headers though: the request header string is indeed - request_header: "POST /servlet/servlet.WebToLead?encoding=UTF-8 HTTP/1.1 ↵Host: www.salesforce.com ↵Accept: / ↵Content-Length: 235 ↵Content-Type: application/x-www-form-urlencoded
I'll mark this as a correct answer. It answers the headers problem even though I'm left confused :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.