14

I am working with PHP curl for post, for some reason I couldn't post the form successfully.

$ch = curl_init();
$headers = [
            'x-api-key: XXXXXX',
            'Content-Type: text/plain'
        ];
$postData = array (
    'data1: value1',
    'data2: value2'
);
curl_setopt($ch, CURLOPT_URL,"XXXXXX");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);           
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$server_output = curl_exec ($ch);

When I try using the same in post it works, but not with PHP.

var_dump($server_output) ==> string(67) ""require data1 and data2 or check the post parameters""
var_dump(curl_error($ch)) ==> string(0) ""
12
  • What do var_dump($server_output) and var_dump(curl_error($ch)) give you? Commented Jul 3, 2017 at 10:04
  • If you're sending text/plain, $postData should be a string, not an array. Commented Jul 3, 2017 at 10:06
  • I suggest you to check this answer stackoverflow.com/a/13596901/1970395 as it also applies to your case. You should build post string from your array instead of passing an array. Commented Jul 3, 2017 at 10:07
  • Json encode your $postData before sending. Commented Jul 3, 2017 at 10:09
  • Are you sure the API wants the parameters in plain text? Most use either url-encoded or JSON. Commented Jul 3, 2017 at 10:09

2 Answers 2

32

If you wanna use Content-type: application/json and raw data, seem your data should be in json format

$ch = curl_init();
$headers  = [
            'x-api-key: XXXXXX',
            'Content-Type: application/json'
        ];
$postData = [
    'data1' => 'value1',
    'data2' => 'value2'
];
curl_setopt($ch, CURLOPT_URL,"XXXXXX");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));           
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result     = curl_exec ($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
Sign up to request clarification or add additional context in comments.

3 Comments

The block array patterns is the key to the solution.
Why is it text/plain in your answer?
I found that the header needs to be 'Content-Type: application/json' in order to process the requests "Raw" body content as JSON
9

If you want an arranged and explained format, see the code below.

// Set The API URL
$url = 'http://www.example.com/api';

// Create a new cURL resource
$ch = curl_init($url);

// Setup request to send json via POST`
$payload = json_encode(array(
    'data1' => 'value1',
    'data2' => 'value2'
   )
);

// Attach encoded JSON string to the POST fields
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);

// Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('x-api-key: XXXXXX', 'Content-Type: application/json'));

// Return response instead of outputting
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Execute the POST request
$result = curl_exec($ch);

// Get the POST request header status
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);

// If header status is not Created or not OK, return error message
if (!$status  || !($status == 201 || $status == 200)) {
   die("Error: call to URL $url failed with status $status, response $result, curl_error " . curl_error($ch) . ", curl_errno " . curl_errno($ch));
}

// Close cURL resource
curl_close($ch);

// if you need to process the response from the API further
$response = json_decode($result, true);

I hope this helps someone

4 Comments

Check on status should be if (!$status || !($status == 201 || $status == 200)) {
Brilliant observation @NickWeavers
Why are both answers setting to Content-Type header to text/plain?
@danronmoon Thanks for the observation, I just updated it to reflect 'application/json'

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.