azukah Posted May 19, 2012 Share Posted May 19, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/262796-post-data-to-url-and-get-response-json-php/ Share on other sites More sharing options...
seanlim Posted May 20, 2012 Share Posted May 20, 2012 In your second attempt, shouldn't the <form> have method="post" too? Is that why there isn't an entry in your access log? Quote Link to comment https://forums.phpfreaks.com/topic/262796-post-data-to-url-and-get-response-json-php/#findComment-1346948 Share on other sites More sharing options...
azukah Posted May 20, 2012 Author Share Posted May 20, 2012 @Seanlim: thanks. i didnt notice that. I added POST as method and still same result Quote Link to comment https://forums.phpfreaks.com/topic/262796-post-data-to-url-and-get-response-json-php/#findComment-1346949 Share on other sites More sharing options...
seanlim Posted May 20, 2012 Share Posted May 20, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/262796-post-data-to-url-and-get-response-json-php/#findComment-1346950 Share on other sites More sharing options...
azukah Posted May 20, 2012 Author Share Posted May 20, 2012 @seanlIm: no, it doees't. It only appears in the web service log when submit via browser (attemp 1). Quote Link to comment https://forums.phpfreaks.com/topic/262796-post-data-to-url-and-get-response-json-php/#findComment-1346951 Share on other sites More sharing options...
seanlim Posted May 20, 2012 Share Posted May 20, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/262796-post-data-to-url-and-get-response-json-php/#findComment-1346952 Share on other sites More sharing options...
azukah Posted May 20, 2012 Author Share Posted May 20, 2012 I do have access to the web service log. Ill try again as soin as i get home and will let u know. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/262796-post-data-to-url-and-get-response-json-php/#findComment-1347036 Share on other sites More sharing options...
azukah Posted May 20, 2012 Author Share Posted May 20, 2012 hi- i added post method to the form and content type to php and still not working. no idea what's wrong <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); } Quote Link to comment https://forums.phpfreaks.com/topic/262796-post-data-to-url-and-get-response-json-php/#findComment-1347080 Share on other sites More sharing options...
seanlim Posted May 20, 2012 Share Posted May 20, 2012 That's the wrong content type. In http_build_query, you are specifying the content type of your POST request, which is application/x-www-form-urlencoded, not what you are requesting for. The web service will specify the json content-type. Quote Link to comment https://forums.phpfreaks.com/topic/262796-post-data-to-url-and-get-response-json-php/#findComment-1347101 Share on other sites More sharing options...
azukah Posted May 20, 2012 Author Share Posted May 20, 2012 oh! makes sense. thanks. i changed it but no luck. 'contentType'=> "application/x-www-form-urlencoded", Quote Link to comment https://forums.phpfreaks.com/topic/262796-post-data-to-url-and-get-response-json-php/#findComment-1347103 Share on other sites More sharing options...
seanlim Posted May 20, 2012 Share Posted May 20, 2012 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... Quote Link to comment https://forums.phpfreaks.com/topic/262796-post-data-to-url-and-get-response-json-php/#findComment-1347106 Share on other sites More sharing options...
azukah Posted May 20, 2012 Author Share Posted May 20, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/262796-post-data-to-url-and-get-response-json-php/#findComment-1347107 Share on other sites More sharing options...
seanlim Posted May 20, 2012 Share Posted May 20, 2012 You missed out the name attribute for your submit button! Put in name="submit" and you should at least get some visible result (or error).. Quote Link to comment https://forums.phpfreaks.com/topic/262796-post-data-to-url-and-get-response-json-php/#findComment-1347114 Share on other sites More sharing options...
azukah Posted May 20, 2012 Author Share Posted May 20, 2012 thanks! i got this error bool(false) when i echo'd $context, i got "Resource id #4" Quote Link to comment https://forums.phpfreaks.com/topic/262796-post-data-to-url-and-get-response-json-php/#findComment-1347116 Share on other sites More sharing options...
seanlim Posted May 20, 2012 Share Posted May 20, 2012 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! Quote Link to comment https://forums.phpfreaks.com/topic/262796-post-data-to-url-and-get-response-json-php/#findComment-1347119 Share on other sites More sharing options...
azukah Posted May 20, 2012 Author Share Posted May 20, 2012 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"} Quote Link to comment https://forums.phpfreaks.com/topic/262796-post-data-to-url-and-get-response-json-php/#findComment-1347120 Share on other sites More sharing options...
azukah Posted May 20, 2012 Author Share Posted May 20, 2012 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 what i need now is to get the ConfirmationCode and no idea how to do that... 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/262796-post-data-to-url-and-get-response-json-php/#findComment-1347131 Share on other sites More sharing options...
seanlim Posted May 20, 2012 Share Posted May 20, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/262796-post-data-to-url-and-get-response-json-php/#findComment-1347134 Share on other sites More sharing options...
azukah Posted May 20, 2012 Author Share Posted May 20, 2012 it works now :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> Quote Link to comment https://forums.phpfreaks.com/topic/262796-post-data-to-url-and-get-response-json-php/#findComment-1347166 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.