title: custom php api with json_encode & json_decode not returning json string
body: <p>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...</p>
  
  <p>Here is a sample of excerpt of my API code..</p>
  
  <pre><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) &gt;= 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 =&gt; $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);
          }
      }
  }
  </code></pre>
  
  <p>I've also created a class to communicate with the API remotely... and this might be where the issue is..</p>
  
  <pre><code>&lt;?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-&gt;postUrl = $this-&gt;apIdomain."?method=".$meth;
      return $this;
  }
  
  function postData($pdata) {
      foreach ($pdata as $key =&gt; $val) {
          if ($key != 'submit') {
              $this-&gt;APData[$key] = $val;
          }
      }
      return $this;
  }
  
  function process() {
      $this-&gt;APData['ipaddress'] = ip2long($_SERVER['REMOTE_ADDR']);
  
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $this-&gt;postUrl);
      curl_setopt($ch, CURLOPT_HEADER, 0);
      curl_setopt($ch, CURLOPT_POST, 1);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $this-&gt;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();
  </code></pre>
  
  <p>and then I have my submitter app just to test it...</p>
  
  <pre><code>&lt;?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-&gt;method('newProspect')-&gt;postData($data)-&gt;process();
  break;
  
  case 'updateProspect':
  
  break;
  
  case 'saveProject':
  break;
  
  case 'finishApplication':
  break;
  }
  </code></pre>
  
  <p>When I run the code by going to <a href="http://www.differentdomain.com/api-test/index.php?method=newProspect" rel="nofollow">http://www.differentdomain.com/api-test/index.php?method=newProspect</a> I get in the browser output: <code>{"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."}</code></p>
  
  <p>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..</p>
  
  <p>Thanks.</p>
  
  <p>new output with change to CURL:</p>
  
  <p><code>object(stdClass)#2 (20) { ["idleads"]=&gt; string(4) "1886" ["classid"]=&gt; string(1) "1" ["ipaddress"]=&gt; string(10) "-949980134" ["register_date"]=&gt; string(19) "0000-00-00 00:00:00" ["name"]=&gt; string(5) "TestB" ["first_name"]=&gt; NULL ["last_name"]=&gt; NULL ["phone"]=&gt; string(12) "555-555-5555" ["phone2"]=&gt; NULL ["email"]=&gt; string(33) "justinblacktest@testerdomain1.com" ["age"]=&gt; NULL ["zip"]=&gt; NULL ["traffic_source"]=&gt; string(1) "1" ["affiliateid"]=&gt; NULL ["sversion"]=&gt; NULL ["purpose"]=&gt; NULL ["amount"]=&gt; NULL ["description"]=&gt; NULL ["respCode"]=&gt; string(3) "200" ["respMsg"]=&gt; string(36) "Duplicate Lead. Information echoed." }</code></p>