dongxinpa3101 2012-05-19 20:26
浏览 42
已采纳

如何使用PHP发送数据到URL而不卷曲

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

Here's my form:

<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>

after clicking submit it takes me to a page with the following JSON output:

{"Prize":"XXXXXX","ConfirmationCode":"######","Error":false,"ErrorMsg":null}

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.

UPDATE i 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"

here's the updated form:

<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>

here's the 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);
}

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

here's the form

<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>

here's the ajax script:

<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>

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.

Here's the form in offer.php

<form action="process.php" method="post" name="myform">
<input id="submit" type="submit" value="submit"/>
</form>

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>
  • 写回答

2条回答 默认 最新

  • duanhan5388 2012-05-19 20:32
    关注

    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">
    

    and then myproxyscript.php would be something like:

    <?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...
    ?>
    

    or you could use file_get_contents in the PHP to fetch the result as given in Tufan's answer.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办