0

When I run this curl code. It gives me a Bad Request Error from Apache Serve. I give a screenshot of this. Also, Give my code.

Please Give me solution Of this.

Screenshot : Bad Request Error Screenshot

My Code is :

$wprest = wp_create_nonce( "wp-rest" );
echo $wprest; 

    $curl = curl_init();
    $data = array('title' => 'REST API insert' , 'content' => 'The content of our stuff');
    $data_string = json_encode($data);

    curl_setopt_array($curl, array(
      CURLOPT_URL => "http://bitechx.com/divithemetest/wp-json/wp/v2/posts",
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 30,
      CURLOPT_CUSTOMREQUEST => "POST",
      CURLOPT_POSTFIELDS => $data_string,
      CURLOPT_HTTPHEADER => array(
            "X-WP-Nonce : ".$wprest,
            "Content-Type: application/json",
            "Cache-Control: no-cache",
            "Content-Length: " . strlen( $data ),
      ),
    ));

    $response = curl_exec($curl);
    $err = curl_error($curl);

    curl_close($curl);

    if ($err) {
      echo "cURL Error #:" . $err;
    } else {
      echo $response;
   }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.