user3820120
user3820120

Reputation: 83

how to create a POST with request in php or javascript

how to create a post with request in php or javascript for steam web API

Example post: https://api.steampowered.com/IEconService/CancelTradeOffer/v1/?key=STEAM_API_KEY&tradeofferid=TRADE_OFFER_ID

when i use it in a browser i get: Method Not Allowed This API must be called with a HTTP POST request

In C# this was written as:

    private bool CancelTradeOffer(ulong tradeofferid)
    {
        string options = string.Format("?key={0}&tradeofferid={1}", ApiKey, tradeofferid);
        string url = String.Format(BaseUrl, "CancelTradeOffer", "v1", options);
        Debug.WriteLine(url);
        string response = SteamWeb.Fetch(url, "POST", null, null, false);
        dynamic json = JsonConvert.DeserializeObject(response);

        if (json == null || json.success != "1")
        {
            return false;
        }
        return true;
    }

Upvotes: 1

Views: 2580

Answers (3)

user3820120
user3820120

Reputation: 83

Final Answer:

PHP Curl Post, works OK :)
Please give credits to WD :)

   $url = 'http://api.steampowered.com/IEconService/CancelTradeOffer/v1/';
    $postData = array();

    $postData['key'] = ""; // insert variable
    $postData['tradeofferid'] = ""; // insert variable

   $fields = '';
   foreach($postData as $key => $value) { 
      $fields .= $key . '=' . $value . '&'; 
   }
   rtrim($fields, '&');


   $post = curl_init();

   curl_setopt($post, CURLOPT_URL, $url);
   curl_setopt($post, CURLOPT_POST, count($postData));
   curl_setopt($post, CURLOPT_POSTFIELDS, $fields);
   curl_setopt($post, CURLOPT_RETURNTRANSFER, 1);

   $result = curl_exec($post);
    var_dump($result);
   curl_close($post);


jQuery Post, works OK :)
Please give credits to SRC :)

<script src="jquery-1.11.1.min.js"></script>

<script>
$(document).ready(function(){
     $.post( "http://api.steampowered.com/IEconService/CancelTradeOffer/v1/", { key: "SteamApiKey", tradeofferid: "TradeOfferID" })
      //This part does not work -- but is not needed to post data
      //.done(function( data ) {
      //  alert( "Data Loaded: " + data );
      //});
})
</script>


To Check Trade Offer status in PHP use:

//Check if trade offer was canceled
    //Get File and avoid error if the server is down
    $CheckTradeOfferID = ""; // add variable
    $BotSteamApiKey = ""; // add variable
    if (!$data = @file_get_contents("https://api.steampowered.com/IEconService/GetTradeOffer/v1/?key=".$BotSteamApiKey."&tradeofferid=".$CheckTradeOfferID."&format=json")) {
        print 'Steam Api is Down';
    } else {
        $json=json_decode($data,true);

        $TradeOffersResponse = $json['response'];
        if (empty($TradeOffersResponse)) { 
            print "Trade Offer ID not found!!!";
        }else{ 
            $trade_offer_state = $json['response']['offer']['trade_offer_state'];

            $TRANSLATE_Trade_Offer_State = "Unknown";
            if($trade_offer_state == "1"){ $TRANSLATE_Trade_Offer_State = "Invalid"; }
            if($trade_offer_state == "2"){ $TRANSLATE_Trade_Offer_State = "Trade Offer Sent"; }
            if($trade_offer_state == "3"){ $TRANSLATE_Trade_Offer_State = "Trade Offer Accepted"; }
            if($trade_offer_state == "4"){ $TRANSLATE_Trade_Offer_State = "The User Sent A Counter Offer"; }
            if($trade_offer_state == "5"){ $TRANSLATE_Trade_Offer_State = "Trade Offer not accepted before the expiration date"; }
            if($trade_offer_state == "6"){ $TRANSLATE_Trade_Offer_State = "The sender cancelled the offer"; }
            if($trade_offer_state == "7"){ $TRANSLATE_Trade_Offer_State = "User Declined the Trade Offer"; }
            if($trade_offer_state == "8"){ $TRANSLATE_Trade_Offer_State = "Some of the items in the offer are no longer available"; }

            print $TRANSLATE_Trade_Offer_State;
        }
    }

Upvotes: -1

SRC
SRC

Reputation: 2271

If you are using jQuery then there is a very handy function to do this.

$.post( "http://api.example.com/get-some-value", { name: "John", time: "2pm" })
  .done(function( data ) {
    alert( "Data Loaded: " + data );
  });

But be careful about cross domain ajax when calling it from JS.

EDIT

For the comment.

You have to include jQuery into your page and then you can call anything within the very useful and handy $( document ).ready() that jQuery supplies.

$(document).ready(function(){
     $.post( "http://api.example.com/get-some-value", { name: "John", time: "2pm" })
      .done(function( data ) {
        alert( "Data Loaded: " + data );
      });
})

Upvotes: 2

Sajeev C
Sajeev C

Reputation: 1538

UPDATE:

Try this:

    $url = 'https://api.steampowered.com/IEconService/CancelTradeOffer/v1/';
    $postData = array();
    $postData['key'] = $STEAM_API_KEY;
    $postData['tradeofferid'] = $TRADE_OFFER_ID;

    $parameters=json_encode($postData);

    $headers = array(    "Accept-Encoding: gzip",
        "Content-Type: application/json");

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
    $resultt = curl_exec($ch);

   var_dump($resultt);
    curl_close($ch);

Or use this as a function to POST values

function httpPost($url,$params)
{
    $postData = '';
    //create name value pairs seperated by &
    foreach($params as $k => $v)
    {
        $postData .= $k . '='.$v.'&';
    }
    rtrim($postData, '&');

    $ch = curl_init();

    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch,CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_POST, count($postData));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

    $output=curl_exec($ch);

    curl_close($ch);
    return $output;

}

Upvotes: 2

Related Questions