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

  • Home
  • SEARCH
  • 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 8657519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:32:26+00:00 2026-06-12T15:32:26+00:00

I currently have an API script that returns JSON. It has worked up until

  • 0

I currently have an API script that returns JSON. It has worked up until I tried to add in a curl php POST script before it. The curl script is working on it’s own, and it is also working in the API script. However the JSON code is not being returned.

Is there something fundamentally wrong with this approach below?

Thanks in advance.

EDIT: The curl script works 100% on its own.
Said script is also working inside the below, it’s just that the JSON does not return.

$name   = "foo";
$age    = "bar";

//set POST variables
$url = 'https://www.example.com';

$fields = array(
            'name' => urlencode($name),
            'age' => urlencode($age)
        );

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);    
return json_encode(
    array(
        "status" => 1,
        "message" => "Success!",
        "request" => 10
    )
);
  • 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-12T15:32:27+00:00Added an answer on June 12, 2026 at 3:32 pm

    You need to do the following use echo and also use CURLOPT_RETURNTRANSFER if not the output would be transferred directly to the page instead of $result

    $name = "foo";
    $age = "bar";
    $url = 'http://.../a.php';
    $fields = array('name' => urlencode($name),'age' => urlencode($age));
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    $result = curl_exec($ch);
    curl_close($ch);
    
    header('Content-type: application/json'); 
    echo json_encode(array("status" => 1,"message" => "Success!","request" => 10));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an API call that returns a byte array. I currently stream the
I currently have a REST API that must be authenticated via BasicAuth, but later
currently I have the following which parses a json api.. import simplejson import urllib2
I have a script that makes calls to an XML api on a remote
I'm currently working on a script that'll be working with Youtubes API. I'm still
I currently have a script that listens for geolocated tweets that include given keywords
Currently I have this small script that makes it possible to check if a
I have a script that calls the Google API server to return the top
I currently have a script that I am trying to use to access an
I currently have a sh script: location=$1 imageNumber=$2 keyword=$3 page=$4 imagesInPage=$5 imagesToGet=$6 imageUrls=`curl -s

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.