fsocket voegt 2000 toe aan resutaat

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Ramon Out

Ramon Out

02/10/2012 10:33:41
Quote Anchor link
Hallo,

Ik maak gebruik van een socket class om bepaalde informatie van bestanden op een andere locatie uit te lezen. Dit resultaat verwerk ik dan weer met reguliere expressies. echter voegt iets in deze class om de zoveel regels de waarde 2000 toe aan het bestand. Het lijkt op een soort buffer issue, maar snap niet helemaal waar het vandaan komt.

Ik heb al bij regel: 380 geprobeerd de 2e variable van fgets te verhogen, maar dit heeft geen effect.

Via een logger functie heb ik het eea uitgelezen en komt de volgende regel daarin:
20121002 10:32:04 result: 179 : <a href="http://twitter.com/home?sta

20121002 10:32:04 result: 180 :2000

20121002 10:32:04 result: 181 :tus=Alles+over+

Hier onder de volledige class:
[code:php]
Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
<?php
include_once("/var/accounts/CN20071192/global_functions/global_functions.php");

/*******************************************
*  Socket Class Made By Lucas van Dijk
* * * * * * * * * * *
*  This class can be used for ANYTHING related to socket
*  programming.
*
*  Includes some examples on how the socket class can be
*  implented. (the HttpGetRequest and HttpPostRequest class)
* * * * * * * * * * *
* Revision 1.3
********************************************/


class Socket
{
    /**
    * @desc the main socket
    * @scope protected
    * @type resource
    */

    var $socket;
    
    /**
    * @desc IP of the server
    * @scope protected
    * @type string
    */

    var $ip;
    
    /**
    * @desc port of the server
    * @scope protected
    * @type int
    */

    var $port;
    
    /**
    * @desc Timeout
    * @scope protected
    * @type int
    */

    var $timeout;
    
    /**
    * @desc Contains an error message
    * @scope protected
    * @type string
    */

    var $error;
    
    /**
    * @desc Contains the data returned
    * @scope protected
    * @type string
    */

    var $contents;
    
    /**
    * @desc Constructor, inits some vars
    * @scope
    */

    function Socket()
    {

        $this -> reset();
    }

    
    /**
    * @desc resets all vars
    * @scope
    */

    function reset()
    {

        $this -> timeout = 10;
        $this -> ip = '';
        $this -> port = 0;
        $this -> contents = '';
    }

    
    /**
    * @desc sets the IP and port
    * @scope
    * @param the IP
    * @param the port
    */

    function set_ip($ip, $port)
    {

        $this -> ip = (string) $ip;
        $this -> port = (int) $port;
    }

    
    /**
    * @desc sets the timeout
    * @scope
    * @param the timeout
    */

    function set_timeout($timeout)
    {

        $this -> timeout = (int) $timeout;
    }

    
    /**
    * @desc gets the error message
    * @scope
    * @return the error message
    */

    function get_error()
    {

        return $this -> error;
    }

    
    /**
    * @desc gets the data returned
    * @scope
    * @return the data
    */

    function get_contents()
    {

        return $this -> contents;
    }

    
    /**
    * @desc connects to the server
    * @scope
    * @return true on success else false
    */

    function connect()
    {

        return $this -> socket = fsockopen($this -> ip, $this -> port, $errno, $this -> error, $this -> timeout);
    }

    
    /**
    * @desc makes the actual request to the server
    * @scope
    * @param the request
    * @return the contents returned by server, false on fail
    */

    function request($request)
    {
        
        // Connect to IP and Port
        $this -> connect();
        
        // Check if connected
        if(is_resource($this -> socket))
        {

            fwrite($this -> socket, $request);
            
            // Get contents
            $buffer = '';
            $i = 0;
            while($buffer = fgets($this -> socket, 4096))
            {

                logger("socketdump.txt", $i."-".$buffer);
                $this -> contents .= $buffer;
$i++;
            }

            
            return $this -> contents;
        }

        else
        {
            // Error!
            return false;
        }
    }
}


class HttpGetRequest extends Socket
{
    /**
    * @desc determines if we should include headers or not
    * @scope protected
    * @type boolean
    */

    var $include_headers;
    
    /**
    * @desc determines if we should redirect, when a Location header was found
    * @scope protected
    * @type boolean
    */

    var $redirect;
    
    /**
    * @desc an array with all the headers to be sent
    * @scope protected
    * @type array
    */

    var $headers;
    
    /**
    * @desc the file to request
    * @scope protected
    * @type string
    */

    var $file;
    
    /**
    * @desc an array with all cookies setted by the web page
    * @scope protected
    * @type array
    */

    var $cookies;
    
    /**
    * @desc Cookies to set with the next request
    * @scope protected
    * @type array
    */

    var $cookies_to_set;
    
    /**
    * @desc constructor, inits some vars
    * @scope
    */

    function HttpGetRequest()
    {

        $this -> reset();
    }

    
    /**
    * @desc resets all vars
    * @scope
    */

    function reset()
    {

        $this -> timeout = 10;
        $this -> include_headers = false;
        $this -> redirect = true;
        $this -> headers = array();
        $this -> file = '';
        $this -> cookies = array();
        $this -> cookies_to_set = array();
        $this -> redirect = true;
    }
        
    
    /**
    * @desc sets if we include headers or not
    * @scope
    * @param true or false, should we include headers?
    */

    function include_headers($yes_no = true)
    {

        $this -> include_headers = (bool) $yes_no;
    }

    
    /**
    * @desc determines if we should redirect, when a Location header was found
    * @scope
    * @param true or false, should we redirect?
    */

    function use_redirect($yes_no = true)
    {

        $this -> redirect = (bool) $yes_no;
    }

    
    /**
    * @desc adds a header
    * @scope
    * @param the header
    */

    function add_header($header)
    {

        $this -> headers[] = (string) $header."\r\n";
    }

    
    /**
    * @desc set the file to request
    * @scope
    * @param the file
    */

    function set_file($file)
    {

        $this -> file = (string) $file;
    }

    
    /**
    * @desc gets the array woth cookies
    * @scope
    * @return array with all the cookies
    */

    function get_cookies()
    {

        return $this -> cookies;
    }

    
    /**
    * @desc send request to the webpage
    * @scope
    * @param the file
    * @return the contents, or false on fail
    */

    function request()
    {

        $contents = '';
        
        // Connect to IP and Port
        $this -> connect();
        
        // Check if connected
        if(is_resource($this -> socket))
        {

            // Build cookie header
            $cookies = '';
            foreach($this -> cookies_to_set as $name => $value)
            {

                $cookies .= $name."=".$value.";";
            }

            
            $this -> add_header("GET /".$this -> file." HTTP/1.1");
            $this -> add_header("Host: ".$this -> ip);
            $this -> add_header("Connection: Close");
            $this -> add_header("User-Agent: Socket PHP class (incompatible; IE sucks)");
            if(count($this -> cookies_to_set))
            {

                $this -> add_header("Cookie: ".$cookies);
            }

            $this -> add_header("");
            
            $this -> contents = $this -> do_request();
            
            $this -> headers = array();
            
            $this -> grab_cookies();
            
            return $this -> contents;
        }

        else
        {
            // Error!
            return false;
        }
    }

    
    /**
    * @desc does the actual request, builds headers etc.
    * @scope protected
    * @return the content
    */

    function do_request()
    {

        $contents = '';
        
        // Build request to send
        $request = '';
        foreach($this -> headers as $header)
        {

            $request .= $header;
        }

        
        fwrite($this -> socket, $request);
        
        if($this -> include_headers == false)
        {

            $line = '';
            while(!($line == "\r\n"))
            {

                $line = fgets($this -> socket, 128);
                
                if($this -> redirect)
                {

                    if(preg_match("#Location\: (.*?)\\r\\n#si", $line, $matches))
                    {

                        $this -> set_file($matches[1]);
                        
                        // Clear all headers
                        $this -> headers = array();
                        return $this -> request();
                    }
                }
            }
    // strip out the header
        }
        if($_SESSION['debug'])
            logger("fsocket2.txt","result: ".print_r($this -> socket, true));
            
        // Get contents
        $buffer = '';
        $i = 0;
        while($buffer = fgets($this -> socket, 20192))
        {

            $contents .= $buffer;
            if($_SESSION['debug'])
                logger("fsocket3.txt","result: ".$i." :".$buffer);
            $i++;
        }
    
        
        if($_SESSION['debug'])
            logger("fsocket.txt","result: ".print_r($contents, true));
            
            
        if($_SESSION['debug'])
          echo "<a href=\"http://baron.struis.com/log/fsocket.txt\" target=\"log\">file 1</a>....<a href=\"http://baron.struis.com/log/fsocket2.txt\" target=\"log\">file 2</a>....<a href=\"http://baron.struis.com/log/fsocket3.txt\" target=\"log\">file 3</a><br />";
          
        return $contents;
    }

    
    /**
    * @desc gets the cookies setted by the webserver
    * @scope protected
    * @return array with the cookies
    */

    function grab_cookies()
    {

        if($this -> include_headers)
        {

            preg_match_all("/Set-Cookie: (.*?)\r\n/si", $this -> contents, $matches, PREG_SET_ORDER);
            
            foreach($matches as $match)
            {

                $cookie = explode(";", $match[1]);
                list($cookie_name, $cookie_value) = explode("=", $cookie[0], 2);
                $this -> cookies[urldecode($cookie_name)] = urldecode($cookie_value);
            }
        }
    }

    
    /**
    * @desc sets a cookie
    * @scope
    * @param cookie name
    * @param cookie value
    */

    function set_cookie($cookie_name, $cookie_value)
    {

        $this -> cookies_to_set[$cookie_name] = $cookie_value;
    }

    
    /**
    * @desc removes a cookie
    * @scope
    * @param cookie name
    */

    function remove_cookie($cookie_name)
    {

        unset($this -> cookies_to_set[$cookie_name]);
    }

    
    /**
    * @desc sets all cookies grabbed by grab_cookies();
    * @scope
    */

    function set_all_cookies()
    {

        foreach($this -> cookies as $name => $value)
        {

            $this -> set_cookie($name, $value);
        }
    }        
}


class HttpPostRequest extends HttpGetRequest
{
    /**
    * @desc Post variables to send
    * @scope protected
    * @type array
    */

    var $post_vars;
    
    /**
    * @desc Constructor, inits some vars
    * @scope
    */

    function HttpPostRequest()
    {

        $this -> reset();
    }

    
    /**
    * @desc resets all vars
    * @scope
    */

    function reset()
    {

        $this -> post_vars = array();
        $this -> timeout = 10;
        $this -> include_headers = false;
        $this -> redirect = true;
        $this -> headers = array();
        $this -> file = '';
        $this -> cookies = array();
        $this -> redirect = true;
    }
        
    
    /**
    * @desc adds a post var
    * @scope
    * @param name of the var
    * @param value of the var
    */

    function add_var($name, $value)
    {

        $this -> post_vars[$name] = $value;
    }

    
    /**
    * @desc makes the actual request to the webpage
    * @scope
    * @param the file
    * @return the contents, or false on fail
    */

    function request()
    {
        
        // Connect to IP and Port
        $this -> connect();
        
        // Check if connected
        if(is_resource($this -> socket))
        {

            // Build post vars
            $vars = '';
            foreach($this -> post_vars as $name => $value)
            {

                $vars .= "&".$name."=".urlencode($value);
            }

            $vars = substr($vars, 1);
            
            $this -> add_header("POST /".$this -> file." HTTP/1.1");
            $this -> add_header("Host: ".$this -> ip);
            $this -> add_header("Connection: Close");
            $this -> add_header("User-Agent: Socket PHP class (incompatible; IE sucks)");
            $this -> add_header("Content-type: application/x-www-form-urlencoded");
            $this -> add_header("Content-Length: ".strlen($vars));
            $this -> add_header('');
            $this -> add_header($vars);
            $this -> add_header('');            
            
            $this -> contents = $this -> do_request();
            
            $this -> grab_cookies();
            
            return $this -> contents;
        }

        else
        {
            // Error!
            return false;
        }
    }
}

?>



Heeft iemand enig idee waar deze 2000 vandaan komt en hoe ik dat kan weghalen. bij voorbaat dank!
 
PHP hulp

PHP hulp

04/05/2024 01:26:13
 
Flip --

Flip --

02/10/2012 11:13:41
Quote Anchor link
niet dat het veel helpt, maar het lijkt wel erg veel op dit topic:
http://www.phphulp.nl/php/forum/topic/linkedin-malformed-json-alles-tussen--backspacen/86726/
 
Ramon Out

Ramon Out

02/10/2012 12:09:42
Quote Anchor link
Lijkt er inderdaad op.... zou kunnen dat die site het toevoegt. gaat hier om eredivisie.nl dus zou best kunnen. bedankt voor reactie in ieder geval.
 
De VeeWee

de VeeWee

02/10/2012 13:54:49
Quote Anchor link
Dit komt volgens mij voor bij chunked HTTP Responses.
http://en.wikipedia.org/wiki/Chunked_transfer_encoding

Deze valt te herkennen door de HTTP header 'Transfer-Encoding:chunked', die inderdaad op de website staat die je vermeld.

De eerste lijn zal dus aangeven hoeveel tekens er staan in het volgende gedeelte.
Als daar dus 2000 staat, dan zal het volgende gedeelte 2000 tekens bevatten.
Na die 2000 tekens staat er dus een nieuw getal, dat aangeeft hoeveel tekens er in het volgende gedeelte zitten.

Je zal dus moeten testen in de headers of het om een volledig document, of een chunked document gaat. Als het om het chunked document gaat, zal je dus de code moeten aanpassen en enkel het aangegeven aantal tekens moeten lezen.
 
Ramon Out

Ramon Out

02/10/2012 14:10:57
Quote Anchor link
Thanks, daar lijkt het inderdaad op. Nu nog vinden hoe ik daar mee om moet gaan, maar weet nu waar ik moet zoeken.
 



Overzicht Reageren

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.