Solved

json POST

Posted on 2013-12-09
13
397 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

0
Comment
Question by:bradley525
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
  • +1
13 Comments
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 39707318
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

0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39708231
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.
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39708247
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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:bradley525
ID: 39708296
Here is the API..I am working off of page 10 right now..
0
 

Author Comment

by:bradley525
ID: 39708297
forgot to attach...Here it is...Thanks
Neurolinq-Plus-API-v1.1--1-.pdf
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39708336
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?
0
 

Author Comment

by:bradley525
ID: 39708344
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
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39708361
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:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_12239-Introduction-to-Application-Programming-Interfaces.html
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39708486
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
0
 

Author Comment

by:bradley525
ID: 39708502
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

0
 

Accepted Solution

by:
bradley525 earned 0 total points
ID: 39708716
I figured out how to get it to work....


//////////////////BIRTHDAY////////////////////////////////
$birth = date('1980-01-01');
$objDateTime = new DateTime($birth);
$date=$objDateTime->format('U');
$date=$date."000"; //add milliseconds required by sitefinity

/////////////////////////////Agreed To Terms/////////////////////////
$agreed_date = date('Y-m-d');;
$aobjDateTime = new DateTime($agreed_date);
$adate=$aobjDateTime->format('U');
$adate=$adate."000"; //add milliseconds required by sitefinity


/////////////////////////ALL THE DATA BELOW IS REQUIRED OR AN ERROR WILL OCCUR////////////////////////////////
$data = 
array( "BillingAddress" => array("PostalCode"=>"xxxxx"),
"Customer" => array(
"CellPhone"=>"xxx-555-1212",
"FirstName"=>"Brad",
"AgreedTermsDate"=>"/Date(".$adate.")/",
"DateOfBirth"=>"/Date(".$date.")/",
"LastName"=>"xxxxxx",
"Password"=>"xxxxxx",
"UserName"=>"levi@xxxxxx.com")); 
       
///////////////////////CONVERT THE ARRAY TO json//////////////////////////////	                                                               
$data_string = json_encode($data);                                                                                   
$ch = curl_init('https://securegateplus.nmp.neuroticmedia.net/Customer/Register');

//curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(   
	'api-key: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',                                                                 
    'Content-Type: application/json; charset=utf-8',                                                                                
    'Content-Length: ' . strlen($data_string))                                                                       
);                                                                                                                   

$result = curl_exec($ch);

//print_r($result);

/////////////////////////CONVERT THE RESULTS INTO AN ARRAY//////////////////////////////
$assocArray = json_decode($result, true);

////SHOW the customer ID////////////////////////
echo $assocArray['CustomerID'];	

Open in new window

0
 

Author Closing Comment

by:bradley525
ID: 39719714
The other solutions were on track, but they did not work. The above solution is tested and works.
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39719976
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!!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question