问题描述
我必须使用PHP发布到URL.现在它可以工作,但是在发送数据/发布之后,它给出了一个json输出(来自我要发布到的Web服务),我需要输出是友好的.我不能将jquery与ajax一起使用,因为它有冲突.我需要直接与Web服务通信
I have to post to a URL using PHP. right now it works but after the sending data/posting, it gives gives a json output (from the web service i'm posting to) and i need the output to be friendly.I can't use jquery with ajax because it's conflicting. i need to communicate directly to the web service
这是我的表格:
<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 name="submit" type="submit" value="submit"/>
</form>
点击提交后,它将带我到具有以下JSON输出的页面:
after clicking submit it takes me to a page with the following JSON output:
{"Prize":"XXXXXX","ConfirmationCode":"######","Error":false,"ErrorMsg":null}
发布正常,因为我查看了Web服务日志,并且提交已注册.我现在要做的就是显示上面格式正确的ConfirmationCode
而不是整个JSON输出.任何帮助将不胜感激.
The posting works ok because i ckeck the web service logs and the submission is registered. all i need to do now is to show the ConfirmationCode
from the piece above nicely formatted instead of the whole JSON output.Any help would be greatly appreciate it.
更新我根据您的帮助进行了一些更改...它刷新页面,但不更新数据库.我检查了Web服务日志,但未记录任何条目.它给我bool(false)作为错误,当回显$ context时,我得到"Resource id#4"
UPDATEi made some changes based on ur help...it refreshes the page but doesn't update the db. i checked the web service log and no entry is recorded.it gives me bool(false) as error and when ECHOing $context, i get "Resource id#4"
这是更新后的表格:
<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 name="submit" type="submit" value="submit"/>
</form>
这是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);
}
更新(带有AJAX)我尝试了ajax,但我在将其写入Web服务时,在clietn站点中遇到了错误.错误显示[object Object]这是ajax
Update (w/AJAX)I tried ajax and i while it writes to the web service, i get an error in the clietn site. the error says [object Object]here's the ajax
这是表格
<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"/>
<div id="result"></div>
这是ajax脚本:
<script type="text/javascript">
$("#submit").click(function(e){
e.preventDefault();
var account = $('#account').val();
var dob = $('#dob').val();
var site = $('#site').val();
$.ajax({
type: "POST",
url: "http://somewebservice.com/<?php echo $PrizeID;?>",
data: "{ 'account': '" + account + "','dob': '" + dob + "', 'site': '" + site + "'}",
contentType: "application/json; charset=utf-8",
async: false,
success: function(data) {
//$('#result').text(data.d);
alert(data);
var obj = $.parseJSON(data);
$("#result").html("<p><b>Confirmation Code:</b> " + obj.ConfirmationCode + "</p>");
},
error: function(data) {
alert(data);
$("#result").html("<b>There was an error. Please try again</b><br/>");
}
});
});
</script>
更新(带卷曲)-已解决-可行!!!! 这项工作有效-它通过网络服务发布到数据库中,并且我可以取回网络服务返回的ConfirmationCode.
UPDATE (w/Curl) - SOLVED - IT WORKS !!!!This works - it posts to the database via the web service and i'm able to get back the ConfirmationCode returned by the webservice.
这是offer.php中的表格
Here's the form in offer.php
<form action="process.php" method="post" name="myform">
<input id="submit" type="submit" value="submit"/>
</form>
这是process.php文件
here's the process.php file
<?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>
推荐答案
您可以创建一个PHP脚本充当Web服务的代理,然后根据需要格式化返回的JSON.
You could create a PHP script to act as a proxy to the web service which then formats the returned JSON as you require.
您的表单标签将更改为:
Your form tag would change to:
<form action="http://myaddress.com/myproxyscript.php" method="post" name="myform">
,然后myproxyscript.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);
// do stuff with result here...
?>
或者您可以在PHP中使用file_get_contents
来获取Tufan答案中给出的结果.
or you could use file_get_contents
in the PHP to fetch the result as given in Tufan's answer.
这篇关于如何使用curl将数据发布到带有php的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!