看了一晚上的php基础,然后通过google的帮助用php curl成功调用了EMS的 Web Service接口,保存几个基本的function,或许以后还能用到:
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 |
# 截取字符串中两个指定标记之间的内容,如果截取的内容还有空格,可以使用ltrim()和rtrim()分别提出左右空格 function str_between($src, $mark1, $mark2) { $index1=strpos($src, $mark1); $index2=strpos($src, $mark2); if(!$index1 || !$index2) return "error"; else{ $index1 += strlen($mark1); return substr($src, $index1, $index2-$index1); } } # post数据,header根据自己的要post的网站要求填写,cookie设置也有好处;注释掉的内容是为了测试 function php_curl_post($url, $postdata) { $c_post=curl_init(); $header = array('Content-Type: text/plain; charset=UTF-8'); curl_setopt($c_post, CURLOPT_URL, $url); curl_setopt($c_post, CURLOPT_RETURNTRANSFER,true); curl_setopt($c_post, CURLOPT_POST,true); curl_setopt($c_post, CURLOPT_HTTPHEADER, $header); curl_setopt($c_post, CURLOPT_POSTFIELDS, $postdata); #curl_setopt($c_post, CURLOPT_BINARYTRANSFER, true); //set cookie curl_setopt($c_post, CURLOPT_COOKIEJAR, 'c:\www\cookie.txt'); curl_setopt($c_post, CURLOPT_COOKIEFILE, 'c:\www\cookie.txt'); // /* $std_out=fopen('c:\www\stdout.txt','a+'); $std_err=fopen('c:\www\stderr.txt','a+'); curl_setopt($c_post, CURLOPT_FILE, $std_out); curl_setopt($c_post, CURLOPT_STDERR, $std_err); curl_setopt($c_post, CURLOPT_VERBOSE,true); */ $result = curl_exec($c_post); curl_close($c_post); /* fclose($std_out); fclose($std_err); */ return $result; } # PUT数据,因为EMS特殊原因,将header和body分开保存到array中,便于提取header和body里面各自所需的内容 function php_curl_put($url, $putdata) { $c_put=curl_init(); $header = array('Content-Type: text/plain; charset=UTF-8'); curl_setopt($c_put, CURLOPT_URL, $url); curl_setopt($c_put, CURLOPT_RETURNTRANSFER,true); curl_setopt($c_put, CURLOPT_HTTPHEADER, $header); curl_setopt($c_put, CURLOPT_POSTFIELDS, $putdata); curl_setopt($c_put, CURLOPT_CUSTOMREQUEST, 'PUT'); curl_setopt($c_put, CURLOPT_HEADER, true); //curl_setopt($c_put, CURLINFO_HEADER_OUT, true); //curl_setopt($c_put, CURLOPT_FOLLOWLOCATION, true); //set cookie curl_setopt($c_put, CURLOPT_COOKIEJAR, 'c:\www\cookie.txt'); curl_setopt($c_put, CURLOPT_COOKIEFILE, 'c:\www\cookie.txt'); // /* $std_out=fopen('c:\www\stdout.txt','a+'); $std_err=fopen('c:\www\stderr.txt','a+'); curl_setopt($c_put, CURLOPT_FILE, $std_out); curl_setopt($c_put, CURLOPT_STDERR, $std_err); curl_setopt($c_put, CURLOPT_VERBOSE,true); */ $result = curl_exec($c_put); $headerSize = curl_getinfo($c_put, CURLINFO_HEADER_SIZE); $header = substr($result, 0 , $headerSize); $body = substr($result, $headerSize); $ret = array('header'=>$header,'body'=>$body); curl_close($c_put); /* fclose($std_out); fclose($std_err); */ return $ret; } # GET数据,没有特殊的地方 function php_curl_get($url) { $c_get=curl_init(); $header = array('Content-Type: text/plain; charset=UTF-8'); curl_setopt($c_get, CURLOPT_URL, $url); #curl_setopt($c_get, CURLOPT_BINARYTRANSFER, true); curl_setopt($c_get, CURLOPT_RETURNTRANSFER,1); curl_setopt($c_get, CURLOPT_HTTPGET,true); //set cookie curl_setopt($c_get, CURLOPT_COOKIEJAR, 'c:\www\cookie.txt'); curl_setopt($c_get, CURLOPT_COOKIEFILE, 'c:\www\cookie.txt'); $result = curl_exec($c_get); curl_close($c_get); return $result; } |
这里没有delete的函数,根据资料,只需改动CURLOPT_CUSTOMREQUEST为"DELETE"一个地方就可以实现delete的功能,所以也没有再写下去 c […]