PHP CURL Using POST Raw JSON Data

前端 未结 2 1728
终归单人心
终归单人心 2020-12-18 00:37

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         


        
2条回答
  •  臣服心动
    2020-12-18 01:22

    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: text/plain'
            ];
    $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);
    

提交回复
热议问题