Jump to content

Post data to URL and get response (JSON, PHP ??)


azukah

Recommended Posts

Hi-

I have to post to a URL, grab one field our of the JSON output/response and format nicely to the user. I've tried a few things with some help but none seem to be working. So any help or direction would be greatly appreciate it!!

 

Attempt 1 (works but not formatted nicely)

if I post using the browser, it works. the data is sent to the URL (web service) and I can see in the web service logs that the entry has been recorded. the problem is that since i have the URL in "action", i'm taken to that page and the user gets to see the JSON output

 

//here's the HTML form in offer.php
<form action="http://somewebservice.com/<?php echo $PrizeID;?>" method="post" name="myform">
<input name="account" id="account" type="hidden" value="<?php echo $account;?>"/>
<input name="dob" id="dob" type="hidden"  value="<?php echo $dob;?>"/>
<input name="site" id="site" type="hidden"  value="<?php echo $site;?>"/>
<input id="submit" type="submit" value="submit"/>
</form>

//here's the output (JSON) after clicking submit
{"Prize":"XXXXXX","ConfirmationCode":"######","Error":false,"ErrorMsg":null}

 

 

Attempt 2 (doens't work)

i tried using php but after submit, the page refreshes and no entry is recorded in the web service log

 

//here's the HTML form in offer.php
<form name="myform">
<input name="account" id="account" type="hidden" value="<?php echo $account;?>"/>
<input name="dob" id="dob" type="hidden"  value="<?php echo $dob;?>"/>
<input name="site" id="site" type="hidden"  value="<?php echo $site;?>"/>
<input id="submit" type="submit" value="submit"/>

//here's the php in offer.php
<?php 
if(isset($_POST['submit'])){
$options = array(
            'http'=>array(
                'method'=>"POST",
                'content'=>http_build_query(array(
                        'account' => $account,
                        'dob' =>   $dob,
                        'site' => $site
                    ))
            ));

        $context = stream_context_create($options);
        $result =  file_get_contents("http://somewebservice.com/{$PrizeID}",NULL,$context);
        var_dump($result);
}
?>

 

Last attempt (doesn't work)

i tried using php & curl so that i can process in the back and show only the ConfirmationCode to the user in a nice format but it doesn't work. after submit, i'm taken to process.php where i see nothing and no entry is recorded in the web service log

 

//here's the HTML form in offer.php
<form action="process.php" method="post" name="myform">
<input name="account" id="account" type="hidden" value="<?php echo $account;?>"/>
<input name="dob" id="dob" type="hidden"  value="<?php echo $dob;?>"/>
<input name="site" id="site" type="hidden"  value="<?php echo $site;?>"/>
<input id="submit" type="submit" value="submit"/>
</form>

//here's the PHP in process.php
<?php 
$postvars=file_get_contents("php://input");
$curl=curl_init("http://somewebservice.com/{$PrizeID}");
curl_setopt($curl,CURLOPT_POSTFIELDS,$postvars);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
$result=curl_exec($curl);
$decodedresult=json_decode($result,true);
?>

 

i haven't gotten to the point where i grab the ConfirmationCode I want because i want to get this fixed first and also because i can't get that to work but will focus on that once i fix this.

Thanks.

Link to comment
Share on other sites

does the request at least appear in the access log now?

 

also (not too sure if it has any effect, but) it should be good to explicitly set the content-type of your http request. i.e. application/x-www-form-urlencoded since you are using http_build_query.

Link to comment
Share on other sites

I am assuming that you do not own "somewebservice.com" and therefore do not have access to their service logs, and the log you are referring to that of your own PHP form?

 

I tried running the snippet you provided for attempt 2, and you ARE indeed required to provide a Content-Type for the request. Apart from that, the code works well and is able to properly generate a HTTP POST request and retrieve the appropriate response.

 

If you have already tried adding a Content-Type to your request and it still doesn't work, I am thinking the error lies somewhere else in offer.php. Try something along the lines of exit()-ing with a message within your if condition just to make sure the first POST is successful.

Link to comment
Share on other sites

hi-

i added post method to the form and content type to php and still not working. no idea what's wrong  :shrug:

<form name="myform" method="post">
<input name="account" id="account" type="hidden" value="<?php echo $account;?>"/>
<input name="dob" id="dob" type="hidden"  value="<?php echo $dob;?>"/>
<input name="site" id="site" type="hidden"  value="<?php echo $site;?>"/>
<input id="submit" type="submit" value="submit"/>
//
if(isset($_POST['submit'])){
$options = array(
            'http'=>array(
                'method'=>"POST",
                'contentType'=> "application/json",
                'content'=>http_build_query(array(
                        'account' => $account,
                        'dob' =>   $dob,
                        'site' => $site
                    ))
            ));

        $context = stream_context_create($options);
        $result =  file_get_contents("http://somewebservice.com/{$PrizeID}",NULL,$context);
        var_dump($result);
}

Link to comment
Share on other sites

Possibly, an invalid HTTP request is being generated. Test this by printing out $context, see if it is formatted correctly.

 

You are also not accessing the POST-ed variables through the global $_POST variable, maybe that's an issue? Or maybe you have omitted that portion of the code...

Link to comment
Share on other sites

this is all the code i have...

 

<?php 
if(isset($_POST['submit'])){
$options = array(
            'http'=>array(
                'method'=>"POST",
                'contentType'=> "application/x-www-form-urlencoded",
                'content'=>http_build_query(array(
                        'account' => $account,
                        'dob' =>   $dob,
                        'site' => $site
                    ))
            ));

        $context = stream_context_create($options);
        $result =  file_get_contents("http://somewebservice.com/{$PrizeID}",NULL,$context);
        var_dump($result);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Reserve</title>
</head>
<body>
<form name="myform" method="post">
<input name="account" id="account" type="hidden" value="<?php echo $account;?>"/>
<input name="dob" id="dob" type="hidden"  value="<?php echo $dob;?>"/>
<input name="site" id="site" type="hidden"  value="<?php echo $site;?>"/>
<input id="submit" type="submit" value="submit"/>
</form>
</body>
</html>

Link to comment
Share on other sites

I think the remaining errors are due to your use of the variables $account, $dob, $site, and $PrizeID.

 

Where are those values defined? Shouldn't you be using $_POST['account'], $_POST['dob'], and $_POST['site'] instead?

 

And is error reporting turned on? If not, turn it on!

Link to comment
Share on other sites

thanks again.

//$PrizeID comes from previous page
//and i changed the the other 3 to 
$_POST['account'];
$_POST['dob'];
$_POST['site'];

 

i also removed the variable and coded the actual values and got the same results.

 

Here's the updated ocde:

<?php 
//get PrzeID
if (isset($_GET['PrizeID'])) {
$PrizeID = $_GET['PrizeID'];
}
//POST
if(isset($_POST['submit'])){
$options = array(
            'http'=>array(
                'method'=>"POST",
                'contentType'=>"application/x-www-form-urlencoded",
                'content'=>http_build_query(array(
                        'account' => $_POST['account'],
                        'dob' =>   $_POST['dob'],
                        'site' => $_POST['site']
                    ))
            ));

        $context = stream_context_create($options);
        $result =  file_get_contents("http://somewebservice.com/{$PrizeID}",NULL,$context);
        var_dump($result);
        $json_result = json_decode($result);

}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
</head>
<body>
<form name="myform" method="post">
<input name="account" id="account" type="hidden" value="<?php echo $account;?>"/>
<input name="dob" id="dob" type="hidden"  value="<?php echo $dob;?>"/>
<input name="site" id="site" type="hidden"  value="<?php echo $site;?>"/>
<input id="submit" type="submit" value="submit"/>
</form>
<div id="result">
<p><?php echo json_encode($_POST);?></p>
</div>
</body>
</html>

 

Here are the results I get when I add name="submit" to the form submit button:

bool(false)
{"account":"300207601","dob":"12\/12\/2012","site":"HLY","submit":"submit"}

 

when i don't have name="submit", i don't get bool(false) and this is the echo for json_encode($_POST)

{"account":"300207601","dob":"12\/12\/2012","site":"HLY"}

Link to comment
Share on other sites

i was playing with php and curl and managed to make it work till it does write to the DB via the web service which is great  :D

what i need now is to get the ConfirmationCode and no idea how to do that...  :shrug: i'll put another post for the confirmationcode part because this has run a bit long.

 

here's the updated code (from attempt 3)

//this is from offer.php file
<form action="process.php" method="post" name="myform">
<input id="submit" type="submit" value="submit"/>
</form>

//here's the php with Curl in process.php
<?php 
$data = array("account" => "{$account}", "dob" => "{$dob}", "site" => "{$site}");                                                                    
$data_string = json_encode($data); 

$ch = curl_init("http://somewebservice.com/{$PrizeID}");                                                                      
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(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string))                                                                       
);                                                                                                                   
$result = curl_exec($ch);                            
curl_close($ch);
?>

Link to comment
Share on other sites

1. You would want to use $_POST['...'] in your <input> fields too.

 

2. Use Content-Type instead of contentType

 

I can't see anything else in your code that could cause the file_get_contents function to return false, apart from the possibility that the server is rejecting the HTTP request and not returning a response.

 

If it still doesn't work, hopefully someone else will be able to spot the problems in your code.

Link to comment
Share on other sites

it works now  :D :D :D

here's the complete code:

 

//this is the submit button in offer.php
<form action="process.php" method="post" name="myform">
<input id="submit" type="submit" value="submit"/>
</form>

//this process.php
<?php
$data = array("account" => "{$account}", "dob" => "{$dob}", "site" => "{$site}");                                                                    
$data_string = json_encode($data); 

$ch = curl_init("http://somewebservice.com/{$PrizeID}");                                                                      
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(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string))                                                                       
);                                                                                                                   
$result = curl_exec($ch);                            
curl_close($ch);
$json_result = json_decode($result, true);
?>
// here's the call for the confirmation number
<p>Your confirmation number is: <strong><?php echo $json_result['ConfirmationCode'];?></strong></p>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.