We help IT Professionals succeed at work.

json POST

bradley525
bradley525 asked
on
426 Views
Last Modified: 2013-12-15
Hello, below is a sample code of data I am trying to post and receive a response.  I would like to do this in PHP but I am not sure how to? If not jQuery would be fine. Any help would be greatly appreciated..Not sure where to start..

POST https://securegateplus.nmp.neuroticmedia.net/Customer/Register HTTP/1.1
api-key: B6FFF46B-9557-42F0-AB88-2FA53C098E84
User-Agent: Fiddler
Host: securegateplus.nmp.neuroticmedia.net
Content-Length: 462
Content-Type: application/json; charset=utf-8
{
"Customer":
{
"UserName":"testers@neuroticmedia.com"
}
}

Open in new window

Comment
Watch Question

Alexandre SimõesSoftware Architect
CERTIFIED EXPERT

Commented:
If it's jQuery and an ajax POST should be something like:
$.ajax({
type:'POST',
url: 'https://securegateplus.nmp.neuroticmedia.net/Customer/Register',
data: {"customer": { "UserName" : "testers@neuroticmedia.com" }},
success: function(){ // handle success },
error: function(){ // handle error }
})

Open in new window


Otherwise (and I'm not a PHP guy) but the idea should be the same as any other <form> POST.
<form action="https://securegateplus.nmp.neuroticmedia.net/Customer/Register" method="post">
        <input name="customer.UserName" type="text" value="testers@neuroticmedia.com" />
        <input name="mySubmit" type="submit" value="submit" />
</form>

Open in new window

Most Valuable Expert 2011
Author of the Year 2014

Commented:
Do you have the documentation that tells what to send to the API?  If so, please post it here or post a link to it, thanks.
CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019

Commented:
You'll only be able to do it with jQuery if the remote server supports JSONP (if not you could push it through a proxy), but as Ray says, without documentation we'll be guessing all day long.

As for PHP, you're probably best off looking a cURL.

We're gonna need more info

Author

Commented:
Here is the API..I am working off of page 10 right now..

Author

Commented:
forgot to attach...Here it is...Thanks
Neurolinq-Plus-API-v1.1--1-.pdf
Most Valuable Expert 2011
Author of the Year 2014

Commented:
Sorry, I just don't have time to read a 62-page API manual.  Is there any kind of a "quickstart" guide or another way we could get to the SSCCE?

Author

Commented:
Sorry .thanks for the help..But I am not asking you to read to 62 pages...I am just on page 10 trying to get the request on the bottom of page 10 to work..thanks
Most Valuable Expert 2011
Author of the Year 2014

Commented:
OK, that shrinks the footprint a bit.  Look at page 1 of the PDF document.  It describes a RESTful access method.  That is what I would recommend.  I'll look at it a little bit more.  While I am doing that, please read this article:
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_12239-Introduction-to-Application-Programming-Interfaces.html
Most Valuable Expert 2011
Author of the Year 2014

Commented:
It looks like you would want to make a cURL POST-method request to this URL:
https://securegateplus.nmp.neuroticmedia.net/Customer/Register

I can't test this, but it may help to get you started.  You might also want to consider fsockopen() to communicate with the API.

<?php // RAY_curl_post_async.php
error_reporting(E_ALL);


// DEMONSTRATE HOW TO USE CURL POST TO START AN ASYNCHRONOUS PROCESS


function curl_post
( $url
, $header
, $post_string
, $timeout=2
, $error_report=FALSE
)
{
    // PREPARE THE CURL CALL
    $curl = curl_init();
    curl_setopt( $curl, CURLOPT_URL,            $url         );
    curl_setopt( $curl, CURLOPT_HEADER,         $header      );
    curl_setopt( $curl, CURLOPT_POST,           TRUE         );
    curl_setopt( $curl, CURLOPT_POSTFIELDS,     $post_string );
    curl_setopt( $curl, CURLOPT_TIMEOUT,        $timeout     );
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, TRUE         );

    // EXECUTE THE CURL CALL
    $htm = curl_exec($curl);
    $err = curl_errno($curl);
    $inf = curl_getinfo($curl);

    // ON FAILURE
    if (!$htm)
    {
        // PROCESS ERRORS HERE
        if ($error_report)
        {
            echo "CURL FAIL: $url TIMEOUT=$timeout, CURL_ERRNO=$err";
            echo "<pre>\n";
            var_dump($inf);
            echo "</pre>\n";
        }
        curl_close($curl);
        return FALSE;
    }

    // ON SUCCESS
    curl_close($curl);
    return $htm;
}


// PREPARE THE JSON STRING
$jso = <<<EOD
{
"BillingAddress":
  {
  "AddressName":"Billing",
  "City":"Newnan",
  "PostalCode":"30076",
  "Province":"GA",
  "Street1":"487 Gibson Rd"
  },
"Customer":
  {
  "AgreedTermsDate":"\/Date(1297110939057-0500)\/",
  "CellPhone":"678-777-3454",
  "DateOfBirth":"\/Date(200462400000-0400)\/",
  "FirstName":"Tim",
  "LastName":"Kohler",
  "MerchantID":"28908a45-750c-4ac6-81e3-98d7a0c889b0",
  "Password":"password",
  "UserName":"testers@neuroticmedia.com"
  }
}
EOD;


// PREPARE THE HEADERS
$hdr[] = 'api-key: B6FFF46B-9557-42F0-AB88-2FA53C098E84';
$hdr[] = 'User-Agent: Fiddler'; // NOT SURE IF THIS MATTERS
$hdr[] = 'Content-Length: ' . strlen($jso);
$hdr[] = 'Content-Type: application/json; charset=utf-8';


// MAKE THE CALL
$resp = curl_post('https://securegateplus.nmp.neuroticmedia.net/Customer/Register', $hdr, $jso, 3, TRUE);
var_dump($resp);

Open in new window

HTH, ~Ray

Author

Commented:
Thank you, but the result says FAIL


CURL FAIL: https://securegateplus.nmp.neuroticmedia.net/Customer/Register TIMEOUT=3, CURL_ERRNO=52
array(22) {
  ["url"]=>
  string(62) "https://securegateplus.nmp.neuroticmedia.net/Customer/Register"
  ["content_type"]=>
  NULL
  ["http_code"]=>
  int(0)
  ["header_size"]=>
  int(0)
  ["request_size"]=>
  int(624)
  ["filetime"]=>
  int(-1)
  ["ssl_verify_result"]=>
  int(0)
  ["redirect_count"]=>
  int(0)
  ["total_time"]=>
  float(0.123081)
  ["namelookup_time"]=>
  float(0.002697)
  ["connect_time"]=>
  float(0.019398)
  ["pretransfer_time"]=>
  float(0.100394)
  ["size_upload"]=>
  float(461)
  ["size_download"]=>
  float(0)
  ["speed_download"]=>
  float(0)
  ["speed_upload"]=>
  float(3745)
  ["download_content_length"]=>
  float(-1)
  ["upload_content_length"]=>
  float(461)
  ["starttransfer_time"]=>
  float(0.123016)
  ["redirect_time"]=>
  float(0)
  ["certinfo"]=>
  array(0) {
  }
  ["redirect_url"]=>
  string(0) ""
}
bool(false)

Open in new window

This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
The other solutions were on track, but they did not work. The above solution is tested and works.
CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019

Commented:
The fact that the other solutions were on track means you should have accepted them as assisted solutions - you only got to where you got to because of the Experts help, so it's only right that you acknolwedge that!!
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.