PHP Code example

Php code posted
created at 18 Apr 10:43

Edit | Back
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
<?php
 
       function get_data($userName, $userPassword, $filter, $process, $verbetype, $updatetype, $requesttype) {
            //encapsulate the credentials into a base64 string
            $authString = base64_encode($userName . ":" . $userPassword);
            $ch = curl_init();
            if($requesttype=="Post")
            {
                  // POST REQUEST
                  if($verbetype=="xml")
                  {
                        $filter=str_replace("<"," &lt;",$filter);
                        $filter=str_replace(">"," &gt;",$filter);
                        $dataToPost="<?xml version=\"1.0\" encoding=\"utf-16\"?><PostRequest xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><updatetype>".$updatetype."</updatetype><filter>".$filter."</filter></PostRequest>";
                  }
                  else
                  {
                        $filter=str_replace('"','\"',$filter);
                        $dataToPost="{\"".updatetype."\":\"Insert\",\"filter\":\"".$filter."\"}";
                  }
                  curl_setopt($ch, CURLOPT_POST, 1);
                  curl_setopt($ch, CURLOPT_POSTFIELDS, $dataToPost);
                  $datapelUrl = "http://salesin.datapel.net/".$verbetype."/" . $process . "?filter~";        
            }
            else
            {
                  // GET REQUEST
                  $dataToPost= "";
                  $datapelUrl = "http://salesin.datapel.net/".$verbetype."/".$process."?filter~".urlencode($filter);
            }
            //init header array
            $header = array();
            //$header[] = 'Content-length:'.strlen($dataToPost);
            $header[] = 'Content-type: application/json';
            $header[] = 'Authorization: '.$authString;


            echo $datapelUrl;
            echo nl2br("\n");
            echo nl2br("\n");    
            //init curl api and send request - return data

            $timeout = 5;

            curl_setopt($ch, CURLOPT_URL, $datapelUrl);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
            $data = curl_exec($ch);
            curl_close($ch);
            return $data;
       }

        // request server timestamp
        echo get_data("developer", "passw0rd",  "", "timestamp", "json", "Query", "GET");
        echo nl2br("\n");

        // request itemlist element by itemnumber match
        echo get_data("developer", "passw0rd", "itemnumber='\c'", "items", "json", "Query", "GET");
        echo nl2br("\n");

?>
2.66 KB in 10 ms with coderay