发送HTTP Post Request PHP

时间:2016-07-14 14:54:15

标签: php api steam

我尝试使用Steam API取消交易要约。在此之后:https://developer.valvesoftware.com/wiki/Steam_Web_API/IEconService#CancelTradeOffer_.28v1.29

我需要使用HTTP Post请求传递tradeofferid。我做了这个,但交易没有取消:

    function RechazarOferta($tradeid)
{
    $data = array('tradeofferid' => $tradeid);
    $trade = array('tradeofferid' => $tradeid, 'language' => 'english');

    $url = 'https://api.steampowered.com/IEconService/CancelTradeOffer/v1/?key=C9786993763535D16XXXXXXXXX&'.http_build_query($trade);

    $c = curl_init();
    curl_setopt($c, CURLOPT_URL, $url);
    curl_setopt($c, CURLOPT_POST, 1);
    curl_setopt($c, CURLOPT_POSTFIELDS, $data);
    $return = curl_exec($c);
    curl_close($c);

    var_dump($return);
}

制作HTTP Post请求的任何解决方案或更好的表单?

2 个答案:

答案 0 :(得分:2)

卷毛非常丑陋和陈旧。 试试Guzzle

$client = new GuzzleHttp\Client();
$res = $client->request('GET', 
   'https://api.steampowered.com/IEconService/CancelTradeOffer/v1', 
   ['query' => [
       'key' =>'C9786993763535D16XXXXXXXXX',
       'tradeofferid' => $tradeid, 
       'language' => 'english']
   ]
);
echo $res->getStatusCode();
// "200"

答案 1 :(得分:0)

我使用这种方法来做,请测试:

$url = 'https://yourweb.com';
$body = 'var1=1&var2=2';

$result = get_data($url, $body);

function get_data($url, $body)
{
    //$userAgent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)';
    $ch      = curl_init();
    $timeout = 7;

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    //curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
    //curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}