2016-09-18 13:52:58 0 Comments PHP Boy.Lee

Fixed Content-Type is empty, when using PHP's CURL lib sending POST request

When using PHP's CURL Lib sending POST request, I found the POST's Content-Type is empty, It's not what I want so I run some debug and fix this issue. Write this topic to memey and hope it can help more people.

{ PHP CURL Config Content-Type }

$headers[] = 'Accept: application/json';
$headers[] = 'Content-Type: application/json; charset=utf-8';

...

if (!empty($headers)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}

 

{ Fixed Content-Type is empty}

The main reason is because POST not carry any content, so php's security strategy direct use empty for Content-Type. The solution is very simple, config some not empty value for POST, then your server side can get correct Content-Type value.

curl_setopt($ch, CURLOPT_POSTFIELDS, ['source'=>'yiilib.com']);