Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8956851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:51:41+00:00 2026-06-15T14:51:41+00:00

Please don’t close this question before read my actual problem. I have seen many

  • 0

Please don’t close this question before read my actual problem. I have seen many related problems but I am still having.
Simply I want to send a json_encoded array by POST method using CURL in PHP. When I set content-type as application/json the data is not posted. I receive empty post array. When I remove content-type line the data is posted but not as json.
Here is my code

 $data = array('id' => $post['id'], 'action' => $post['action']);
 $post_data = http_build_query($data);
 curl_setopt($ch, CURLOPT_URL, $url);
 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
 curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
 curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data));

I this case I receive empty post. If I comment out the line curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); I will receive the post data but not json encoded. I want json_encoded array. Will any one tell me what am I doing wrong?

Best Regards:

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:51:42+00:00Added an answer on June 15, 2026 at 2:51 pm

    You are encoding a query string to json not an array remove $post_data = http_build_query($data); and pass $data to json_encode.

     $data = array('id' => $post['id'], 'action' => $post['action']);
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
     curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
     curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    

    ofcourse for this to work the service nedds to support json posts.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please don't think it's a redundant question. Please read completely. I have an infinite
There is some precedent for search-engine-ranking-related questions on StackOverflow, so please don't close this
Full disclosure: this is for an assignment, so please don't post actual code solutions!
Please don't be deterred by the just another IE problem. I have a custom
Ok I don't want this to be a hacking advice question, so please don't
I have a very amateurish question, so please don't bomb me. I work in
I have a bunch of php arrays that look like this (please don't ask
Firstly, please don't dismiss this question - I'm aware it's an ugly situation but
** Please don't migrate this to Apple - it won't be seen. Thanks. I'm
OK. this might be a very basic question to please don't flame me but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.