Curl Combineren
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
$url = sprintf("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=%s&steamids=%s",
$api_key,
$steam_id);
$curl_time = microtime(1);
// Needs curl to actually be able to pull data from steam api because of the special response-behaviour.
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
@curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_MAXREDIRS, 5 );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 3 ); #rc1: helps with page delay if steam is down
$raw_json = curl_exec( $ch );
$http_response_header = curl_getinfo( $ch );
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
curl_close($ch);
$url = sprintf("http://api.steampowered.com/IPlayerService/GetSteamLevel/v0001/?key=%s&steamid=%s",
$api_key,
$steam_id);
$curl_time = microtime(1);
// Needs curl to actually be able to pull data from steam api because of the special response-behaviour.
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
@curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_MAXREDIRS, 5 );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 2 ); #rc1: helps with page delay if steam is down
$steamlevel = curl_exec( $ch );
$http_response_header = curl_getinfo( $ch );
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
curl_close($ch);
$steamlevel = json_decode($steamlevel, 1);
// Get Player Bans API
$url = sprintf("http://api.steampowered.com/ISteamUser/GetPlayerBans/v1/?key=%s&steamids=%s",
$api_key,
$steam_id);
$curl_time = microtime(1);
// Needs curl to actually be able to pull data from steam api because of the special response-behaviour.
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
@curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_MAXREDIRS, 5 );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 2 ); #rc1: helps with page delay if steam is down
$tradeban = curl_exec( $ch );
$http_response_header = curl_getinfo( $ch );
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
curl_close($ch);
$tradeban = json_decode($tradeban, 1);
// END Get Player Bans API
// Get Item Info API //
$url = sprintf("http://api.steampowered.com/IEconItems_440/GetPlayerItems/v0001/?key=%s&steamid=%s",
$api_key,
$steam_id);
$curl_time = microtime(1);
// Needs curl to actually be able to pull data from steam api because of the special response-behaviour.
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
@curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_MAXREDIRS, 5 );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 2 ); #rc1: helps with page delay if steam is down
$getplayeritems = curl_exec( $ch );
$http_response_header = curl_getinfo( $ch );
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
curl_close($ch);
$getplayeritems = json_decode($getplayeritems, 1);
// END Get Item Info API //
$api_key,
$steam_id);
$curl_time = microtime(1);
// Needs curl to actually be able to pull data from steam api because of the special response-behaviour.
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
@curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_MAXREDIRS, 5 );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 3 ); #rc1: helps with page delay if steam is down
$raw_json = curl_exec( $ch );
$http_response_header = curl_getinfo( $ch );
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
curl_close($ch);
$url = sprintf("http://api.steampowered.com/IPlayerService/GetSteamLevel/v0001/?key=%s&steamid=%s",
$api_key,
$steam_id);
$curl_time = microtime(1);
// Needs curl to actually be able to pull data from steam api because of the special response-behaviour.
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
@curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_MAXREDIRS, 5 );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 2 ); #rc1: helps with page delay if steam is down
$steamlevel = curl_exec( $ch );
$http_response_header = curl_getinfo( $ch );
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
curl_close($ch);
$steamlevel = json_decode($steamlevel, 1);
// Get Player Bans API
$url = sprintf("http://api.steampowered.com/ISteamUser/GetPlayerBans/v1/?key=%s&steamids=%s",
$api_key,
$steam_id);
$curl_time = microtime(1);
// Needs curl to actually be able to pull data from steam api because of the special response-behaviour.
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
@curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_MAXREDIRS, 5 );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 2 ); #rc1: helps with page delay if steam is down
$tradeban = curl_exec( $ch );
$http_response_header = curl_getinfo( $ch );
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
curl_close($ch);
$tradeban = json_decode($tradeban, 1);
// END Get Player Bans API
// Get Item Info API //
$url = sprintf("http://api.steampowered.com/IEconItems_440/GetPlayerItems/v0001/?key=%s&steamid=%s",
$api_key,
$steam_id);
$curl_time = microtime(1);
// Needs curl to actually be able to pull data from steam api because of the special response-behaviour.
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
@curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_MAXREDIRS, 5 );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 2 ); #rc1: helps with page delay if steam is down
$getplayeritems = curl_exec( $ch );
$http_response_header = curl_getinfo( $ch );
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
curl_close($ch);
$getplayeritems = json_decode($getplayeritems, 1);
// END Get Item Info API //
Ik merk op dat door vele API's te gebruiken van steam, dat het laden van de pagina echter lang duurt. Ik probeer dit dus te combineren in 1 code, i.p.v. 4x een curl_close($ch);. Met hopelijk dat ik een optimized code kan gebruiken om de laad-tijd te verbeteren. Met PHP ben ik nog nieuw, dus ik doe mij best :).
Wie kan mij hierbij helpen? Ik heb al verschilende soorten curl's geprobeerd, maar het lukt me maar niet.
Gewijzigd op 11/06/2015 21:17:19 door Domien Brandsma
Dat moet gewoon met 4 acties.
Normaal kan dat ook met file_get_contents(), maar dat zal niet werken, gezien deze regel (regel 79 onder andere):
Code (php)
1
2
3
2
3
<?php
// Needs curl to actually be able to pull data from steam api because of the special response-behaviour.
?>
// Needs curl to actually be able to pull data from steam api because of the special response-behaviour.
?>
Je hebt dus 4 accties nodig.
Al kan je wel af met (veel) minder code.
Voor de rest ziet het er prima uit.
Wat is het probleem?
Je kan de curl-close() ook gewoon weghalen... dan blijft je connectie open.
Dat kan handig zijn als je inlogt ergens (actie 1), dan een pagina opent (actie 2) en daarna iets wil plaatsen (actie 3).
CURL is handig voor dat soort dingen, want je kan ook cookies/sessies etc krijgen tijdens je bezoek die je dan 'onthoudt'.
Code (php)
1
a:11:{s:7:"steamid";s:17:"76561197994195927";s:24:"communityvisibilitystate";s:1:"3";s:11:"personaname";s:13:"MasterSoprano";s:10:"profileurl";s:39:"http://steamcommunity.com/id/MrSoprano/";s:6:"avatar";s:116:"https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/a1/a101f855bceb7cf565482632fbc3d310b5dd4bc3.jpg";s:12:"avatarmedium";s:123:"https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/a1/a101f855bceb7cf565482632fbc3d310b5dd4bc3_medium.jpg";s:10:"avatarfull";s:121:"https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/a1/a101f855bceb7cf565482632fbc3d310b5dd4bc3_full.jpg";s:11:"timecreated";s:10:"1195099352";s:12:"player_level";i:29;s:10:"EconomyBan";s:4:"none";s:15:"backpack_status";i:1;}
Dat terwijl ik de thread zo heb ingesteld dat ie maar 5/10 posts per pagina laat zien duurt het alsnog te lang.
Zie: http://pastebin.com/LUkzbfAv voor de hele code.
Gewijzigd op 11/06/2015 21:57:57 door Domien Brandsma
Als je zegt dat je elke keer als een thread wordt geopend, de API checkt.
Dat moet je dus niet doen: je moet de cache openen.
En op de achtergrond (of eens per 10 minuten oid) de cache bijwerken.
Daar merken je gebruikers/bezoekers niets van, want zij hoeven niet te wachten.
Na die dag zal een cron alle cache checken om te zien wat er "out-of-date" is, en behouden en of verwijderen.
Gewijzigd op 11/06/2015 22:32:03 door Domien Brandsma