Title / Description
Code <?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("<"," <",$filter); $filter=str_replace(">"," >",$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"); ?>
Author
Highlight as C C++ CSS Clojure Delphi ERb Groovy (beta) HAML HTML JSON Java JavaScript PHP Plain text Python Ruby SQL XML YAML diff code