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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:46:34+00:00 2026-05-31T14:46:34+00:00

I’ve recently written a custom API for inserting data into my database from multiple

  • 0

I’ve recently written a custom API for inserting data into my database from multiple websites across different servers… But it’s having some issues and I don’t know what it could be…

Here is a sample of excerpt of my API code..

if (function_exists($_GET['method'])) {
        // function exists, so lets run it.
        $_GET['method']();
    } else {
        // function does not exist so lets throw an error.
        $data['respCode'] = "100";
        $data['respMsg'] = "The method you have called does not exist.  Please reformat your call.";
        echo json_encode($data); 
    }

// methods

function newProspect() {
    // lets first check for the lead in the database..

    $sql = mysql_query("SELECT * FROM leads WHERE email = '".$_POST['email']."' LIMIT 1");
    if (mysql_num_rows($sql) >= 1) {
        // duplicate found, so lets just send the data..
        $data = mysql_fetch_assoc($sql);
        $data['respCode'] = "200";
        $data['respMsg'] = "Duplicate Lead.  Information echoed.";
        echo json_encode($data);
    } else {
        // no duplicate found, so lets insert the data..
        $sql = "INSERT INTO leads SET ";
        foreach ($_POST as $key => $value) {
            $sql .= $key." = '".$value."',";
        }
        $sql .= "register_date = '".$rdate."'"; 
        $sql = mysql_query($sql);
        if (!$sql) {
            // could not insert the info into the database so lets throw an error.
            $data['respCode'] = "102";
            $data['respMsg'] = "Could not intert into database.";
            $data['errorMsg'] = mysql_error();

            echo json_encode($data); return json_encode($data);
        } else {
            // lead was inserted into the database just fine, so lets pass back the data.
            $id = mysql_insert_id($sql);
            $sql = mysql_query("SELECT * FROM leads WHERE `idleads` =".$id."");
            $data = mysql_fetch_assoc($id);
            $data['respCode'] = '200';
            $data['respMsg'] = "Lead Entered into Database Successfully.";

            echo json_encode($data);
        }
    }
}

I’ve also created a class to communicate with the API remotely… and this might be where the issue is..

<?php

class theApi {
public $apIdomain = 'http://www.domain.com/api/index.php'; // ie: https://www.mydomain.com/admin/

public $APData = array();
public $postUrl = '';

public function __construct() {

}

function method ($meth = 'newProspect') {
    $this->postUrl = $this->apIdomain."?method=".$meth;
    return $this;
}

function postData($pdata) {
    foreach ($pdata as $key => $val) {
        if ($key != 'submit') {
            $this->APData[$key] = $val;
        }
    }
    return $this;
}

function process() {
    $this->APData['ipaddress'] = ip2long($_SERVER['REMOTE_ADDR']);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $this->postUrl);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $this->APData);   
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,0); 
    curl_setopt($ch, CURLOPT_TIMEOUT,30);

    $rawresponse = curl_exec($ch);

    curl_close($ch);

    return $rawresponse;

}
}

$ap = new theApi();

and then I have my submitter app just to test it…

<?php
$_method = $_GET['method'];
require_once("api.php");
switch ($_method) {

    case 'newProspect':
        $data['name'] = "TestB";
    $data['phone'] = "555-555-5555";
    $data['email'] = "testB@testerdomain1.com";

    $d = $ap->method('newProspect')->postData($data)->process();
break;

case 'updateProspect':

break;

case 'saveProject':
break;

case 'finishApplication':
break;
}

When I run the code by going to http://www.differentdomain.com/api-test/index.php?method=newProspect I get in the browser output: {"idleads":"1886","classid":"1","ipaddress":"-949980134","register_date":"0000-00-00 00:00:00","name":"TestB","first_name":null,"last_name":null,"phone":"555-555-5555","phone2":null,"email":"testB@testerdomain1.com","age":null,"zip":null,"traffic_source":"1","affiliateid":null,"sversion":null,"purpose":null,"amount":null,"description":null,"respCode":"200","respMsg":"Duplicate Lead. Information echoed."}

So the API itself is working… But I can’t run json_decode anywhere and it almost appears as though CURL isn’t getting the data at all back from the API… any help on this is greatly appreciated.. I have no clue where to go from here to get it to work..

Thanks.

new output with change to CURL:

object(stdClass)#2 (20) { ["idleads"]=> string(4) "1886" ["classid"]=> string(1) "1" ["ipaddress"]=> string(10) "-949980134" ["register_date"]=> string(19) "0000-00-00 00:00:00" ["name"]=> string(5) "TestB" ["first_name"]=> NULL ["last_name"]=> NULL ["phone"]=> string(12) "555-555-5555" ["phone2"]=> NULL ["email"]=> string(33) "justinblacktest@testerdomain1.com" ["age"]=> NULL ["zip"]=> NULL ["traffic_source"]=> string(1) "1" ["affiliateid"]=> NULL ["sversion"]=> NULL ["purpose"]=> NULL ["amount"]=> NULL ["description"]=> NULL ["respCode"]=> string(3) "200" ["respMsg"]=> string(36) "Duplicate Lead. Information echoed." }

  • 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-31T14:46:36+00:00Added an answer on May 31, 2026 at 2:46 pm

    Set CURL_RETURNTRANSFER to 1 to return the HTTP request output, rather than echoing it to the screen.

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $this->postUrl);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $this->APData);   
    // Set to 1
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
    curl_setopt($ch, CURLOPT_TIMEOUT,30);
    
    // now $rawresponse actually contains the JSON retrieved
    // from the API call.
    $rawresponse = curl_exec($ch);
    

    This is mentioned in the curl_exec() documentation.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am currently running into a problem where an element is coming back from
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string

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.