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 7368407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:40:04+00:00 2026-05-29T03:40:04+00:00

I am trying to pass a json data as param for cURL POST. However,

  • 0

I am trying to pass a json data as param for cURL POST. However, I am stuck at grabbing it and saving it on db.

cURL file:

$data = array("name" => "Hagrid", "age" => "36");                                                                    
$data_string = json_encode($data);                                                                                   

$url = 'http://localhost/project/test_curl';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
                                    'Content-Type: application/json')                                                                                           
                                    );                       
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                                                     

$result = curl_exec($ch);  

//based on http://www.lornajane.net/posts/2011/posting-json-data-with-php-curl

test_curl file:

    $order_info = $_POST; // this seems to not returning anything

    //SAVE TO DB... saving empty...

What did I miss? Weew….

  • 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-05-29T03:40:04+00:00Added an answer on May 29, 2026 at 3:40 am

    You are sending the data as raw JSON in the body, it will not populate the $_POST variable.

    You need to do one of two things:

    1. You can change the content type to one that will populate the $_POST array
    2. You can read the raw body data.

    I would recommend option two if you have control over both ends of the communication, as it will keep the request body size to a minimum and save bandwidth over time. (Edit: I didn’t really emphasize here that the amount of bandwidth it will save is negligible, only a few bytes per request, this would only be a valid concern is very high traffic environments. However I still recommend option two because it is the cleanest way)

    In your test_curl file, do this:

    $fp = fopen('php://input', 'r');
    $rawData = stream_get_contents($fp);
    
    $postedJson = json_decode($rawData);
    
    var_dump($postedJson);
    

    If you want to populate the $_POST variable, you will need to change the way you send the data to the server:

    $data = array (
      'name' => 'Hagrid',
      'age' => '36'
    );
    
    $bodyData = array (
      'json' => json_encode($data)
    );
    $bodyStr = http_build_query($bodyData);
    
    $url = 'http://localhost/project/test_curl';
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
      'Content-Type: application/x-www-form-urlencoded',
      'Content-Length: '.strlen($bodyStr)
    ));
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyStr);
    
    $result = curl_exec($ch);
    

    The raw, undecoded JSON will now be available in $_POST['json'].

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

Sidebar

Related Questions

I'm trying to pass an array to json.stringify but the returned value is coming
I am trying to pass JSON data from the client browser to an ASP.NET
I am trying to fetch JSON data every 2 seconds and pass it to
I am having problems trying to POST a JSON Array. For my Android code,
I'm trying to pass data to an MVC post action that is expecting 1
I am currently trying to pass a generated JSON string to dojo for parsing
is there a way i can pass json format data through django HttpResponse. I
I'm desperately trying to use json data to fill my dojo spider chart. First
I'm trying to pass an array from a jQuery function to my controller. The
I'm trying to pass some data to javascript in my view. I only want

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.