1

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'] = "[email protected]";

    $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":"[email protected]","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) "[email protected]" ["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 Answer 1

2

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.

3
  • that kinda worked... but i can't run json_decode on the output now.. I put the new output when i ran var_dump in my OP @ the bottom.. Commented Mar 21, 2012 at 17:02
  • @Johnny The output you posted has already been decoded into an object. You can access it like $obj->idleads (1186) Commented Mar 21, 2012 at 17:06
  • Yeah I saw that... I tried that and it didn't work either.. But I fixed it.. Thanks a ton! Commented Mar 21, 2012 at 17:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.