dongxinpa3101
dongxinpa3101
2012-05-19 20:26

如何使用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 duanhan5388 9年前

    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.

    点赞 评论 复制链接分享
  • duanba7653 duanba7653 9年前

    if you want to do a POST request without curl, you can use, file_get_contents() and other file operations (fopen etc.) create a context and set default context options ,

    Example :

     $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);
    
    点赞 评论 复制链接分享

相关推荐