@@ -7,138 +7,138 @@ discard block |
||
| 7 | 7 | require_once "PUnit.php"; |
| 8 | 8 | require_once "../lib/RequestCore.class.php"; |
| 9 | 9 | class SDKTest extends PUnit{ |
| 10 | - protected $bucket = "php-sdk-test"; |
|
| 11 | - protected $key = "test==中/文?"; |
|
| 12 | - protected $key_copy = "test中/文_copy?"; |
|
| 13 | - protected $accesskey = ""; |
|
| 14 | - protected $secrectkey = ""; |
|
| 15 | - protected $client; |
|
| 10 | + protected $bucket = "php-sdk-test"; |
|
| 11 | + protected $key = "test==中/文?"; |
|
| 12 | + protected $key_copy = "test中/文_copy?"; |
|
| 13 | + protected $accesskey = ""; |
|
| 14 | + protected $secrectkey = ""; |
|
| 15 | + protected $client; |
|
| 16 | 16 | protected $encryptionClient; |
| 17 | - protected $cachedir; |
|
| 17 | + protected $cachedir; |
|
| 18 | 18 | protected $sseckey; |
| 19 | - public function __construct(){ |
|
| 20 | - $this->client=new Ks3Client($this->accesskey,$this->secrectkey); |
|
| 21 | - $this->cachedir=KS3_API_PATH.DIRECTORY_SEPARATOR."unit".DIRECTORY_SEPARATOR."cache".DIRECTORY_SEPARATOR; |
|
| 19 | + public function __construct(){ |
|
| 20 | + $this->client=new Ks3Client($this->accesskey,$this->secrectkey); |
|
| 21 | + $this->cachedir=KS3_API_PATH.DIRECTORY_SEPARATOR."unit".DIRECTORY_SEPARATOR."cache".DIRECTORY_SEPARATOR; |
|
| 22 | 22 | $filename = "secret.key"; |
| 23 | 23 | $handle = fopen($filename, "r"); |
| 24 | 24 | $sseckey = fread($handle, filesize ($filename)); |
| 25 | 25 | fclose($handle); |
| 26 | 26 | $this->sseckey = $sseckey; |
| 27 | 27 | $this->encryptionClient = new Ks3EncryptionClient($this->accesskey,$this->secrectkey,$sseckey); |
| 28 | - } |
|
| 29 | - public function before(){ |
|
| 30 | - if($this->client->bucketExists(array("Bucket"=>$this->bucket))){ |
|
| 31 | - $keys = array(); |
|
| 32 | - $objects = $this->client->listObjects(array("Bucket"=>$this->bucket)); |
|
| 33 | - foreach ($objects["Contents"] as $object) { |
|
| 34 | - array_push($keys, $object["Key"]); |
|
| 35 | - } |
|
| 36 | - $this->client->deleteObjects(array("Bucket"=>$this->bucket,"DeleteKeys"=>$keys)); |
|
| 37 | - }else{ |
|
| 38 | - $this->client->createBucket(array("Bucket"=>$this->bucket)); |
|
| 39 | - } |
|
| 40 | - } |
|
| 41 | - public function testListBuckets(){ |
|
| 42 | - $buckets = $this->client->listBuckets(); |
|
| 43 | - $found = FALSE; |
|
| 44 | - foreach ($buckets as $bucket) { |
|
| 45 | - if($bucket["Name"] == $this->bucket) |
|
| 46 | - $found = TRUE; |
|
| 47 | - } |
|
| 48 | - if(!$found) |
|
| 49 | - throw new Exception("list buckets expected found ".$this->bucket.",but not found"); |
|
| 28 | + } |
|
| 29 | + public function before(){ |
|
| 30 | + if($this->client->bucketExists(array("Bucket"=>$this->bucket))){ |
|
| 31 | + $keys = array(); |
|
| 32 | + $objects = $this->client->listObjects(array("Bucket"=>$this->bucket)); |
|
| 33 | + foreach ($objects["Contents"] as $object) { |
|
| 34 | + array_push($keys, $object["Key"]); |
|
| 35 | + } |
|
| 36 | + $this->client->deleteObjects(array("Bucket"=>$this->bucket,"DeleteKeys"=>$keys)); |
|
| 37 | + }else{ |
|
| 38 | + $this->client->createBucket(array("Bucket"=>$this->bucket)); |
|
| 39 | + } |
|
| 40 | + } |
|
| 41 | + public function testListBuckets(){ |
|
| 42 | + $buckets = $this->client->listBuckets(); |
|
| 43 | + $found = FALSE; |
|
| 44 | + foreach ($buckets as $bucket) { |
|
| 45 | + if($bucket["Name"] == $this->bucket) |
|
| 46 | + $found = TRUE; |
|
| 47 | + } |
|
| 48 | + if(!$found) |
|
| 49 | + throw new Exception("list buckets expected found ".$this->bucket.",but not found"); |
|
| 50 | 50 | |
| 51 | - } |
|
| 52 | - public function testDeleteBucket(){ |
|
| 53 | - $this->client->putObjectByContent(array("Bucket"=>$this->bucket,"Key"=>"test","Content"=>"")); |
|
| 54 | - $ex = NULL; |
|
| 55 | - try{ |
|
| 56 | - $this->client->deleteBucket(array("Bucket"=>$this->bucket)); |
|
| 57 | - }catch(Exception $e){ |
|
| 58 | - $ex = $e; |
|
| 59 | - } |
|
| 60 | - if($ex == NULL||!($ex->errorCode === "BucketNotEmpty")){ |
|
| 61 | - throw new Exception("delete bucket expected BucketNotEmpty but ".$ex); |
|
| 62 | - } |
|
| 63 | - } |
|
| 64 | - public function testBucketCORS(){ |
|
| 65 | - $this->client->setBucketCORS($args = array( |
|
| 66 | - "Bucket"=>$this->bucket, |
|
| 67 | - "CORS"=>array( |
|
| 68 | - array( |
|
| 69 | - "AllowedMethod"=>array("GET","PUT"), |
|
| 70 | - "AllowedOrigin"=>array("http://www.kingsoft.com"), |
|
| 71 | - "AllowedHeader"=>array("*"), |
|
| 72 | - "ExposeHeader"=>array("*"), |
|
| 73 | - "MaxAgeSeconds"=>10 |
|
| 74 | - ), |
|
| 75 | - array( |
|
| 76 | - "AllowedMethod"=>array("GET","PUT"), |
|
| 77 | - "AllowedOrigin"=>array("*"), |
|
| 78 | - "AllowedHeader"=>array("*"), |
|
| 79 | - "ExposeHeader"=>array("*"), |
|
| 80 | - "MaxAgeSeconds"=>10 |
|
| 81 | - ) |
|
| 82 | - ))); |
|
| 83 | - $cors = $this->client->getBucketCORS(array("Bucket"=>$this->bucket)); |
|
| 84 | - $this->assertEquals(count($cors),2,"bucket cors count "); |
|
| 85 | - $this->client->deleteBucketCORS(array("Bucket"=>$this->bucket)); |
|
| 86 | - $cors = $this->client->getBucketCORS(array("Bucket"=>$this->bucket)); |
|
| 87 | - $this->assertEquals(count($cors),0,"bucket cors count "); |
|
| 88 | - } |
|
| 89 | - public function testCreateBucket(){ |
|
| 90 | - $ex = NULL; |
|
| 91 | - try{ |
|
| 92 | - $this->client->createBucket(array("Bucket"=>$this->bucket)); |
|
| 93 | - }catch(Exception $e){ |
|
| 94 | - $ex = $e; |
|
| 95 | - } |
|
| 96 | - if($ex == NULL||!($ex->errorCode === "BucketAlreadyExists")){ |
|
| 97 | - throw new Exception("create bucket expected BucketAlreadyExists but ".$ex); |
|
| 98 | - } |
|
| 99 | - } |
|
| 100 | - public function testACL(){ |
|
| 101 | - $this->client->setBucketAcl(array("Bucket"=>$this->bucket,"ACL"=>"public-read")); |
|
| 102 | - $acl = $this->client->getBucketAcl(array("Bucket"=>$this->bucket)); |
|
| 103 | - $this->assertEquals($acl,"public-read","bucket acl"); |
|
| 104 | - } |
|
| 105 | - public function testBucketLogging(){ |
|
| 106 | - $this->client->setBucketLogging(array( |
|
| 107 | - "Bucket"=>$this->bucket, |
|
| 108 | - "BucketLogging"=>array( |
|
| 109 | - "Enable"=>TRUE, |
|
| 110 | - "TargetBucket"=>$this->bucket, |
|
| 111 | - "TargetPrefix"=>"X-KSS" |
|
| 51 | + } |
|
| 52 | + public function testDeleteBucket(){ |
|
| 53 | + $this->client->putObjectByContent(array("Bucket"=>$this->bucket,"Key"=>"test","Content"=>"")); |
|
| 54 | + $ex = NULL; |
|
| 55 | + try{ |
|
| 56 | + $this->client->deleteBucket(array("Bucket"=>$this->bucket)); |
|
| 57 | + }catch(Exception $e){ |
|
| 58 | + $ex = $e; |
|
| 59 | + } |
|
| 60 | + if($ex == NULL||!($ex->errorCode === "BucketNotEmpty")){ |
|
| 61 | + throw new Exception("delete bucket expected BucketNotEmpty but ".$ex); |
|
| 62 | + } |
|
| 63 | + } |
|
| 64 | + public function testBucketCORS(){ |
|
| 65 | + $this->client->setBucketCORS($args = array( |
|
| 66 | + "Bucket"=>$this->bucket, |
|
| 67 | + "CORS"=>array( |
|
| 68 | + array( |
|
| 69 | + "AllowedMethod"=>array("GET","PUT"), |
|
| 70 | + "AllowedOrigin"=>array("http://www.kingsoft.com"), |
|
| 71 | + "AllowedHeader"=>array("*"), |
|
| 72 | + "ExposeHeader"=>array("*"), |
|
| 73 | + "MaxAgeSeconds"=>10 |
|
| 74 | + ), |
|
| 75 | + array( |
|
| 76 | + "AllowedMethod"=>array("GET","PUT"), |
|
| 77 | + "AllowedOrigin"=>array("*"), |
|
| 78 | + "AllowedHeader"=>array("*"), |
|
| 79 | + "ExposeHeader"=>array("*"), |
|
| 80 | + "MaxAgeSeconds"=>10 |
|
| 81 | + ) |
|
| 82 | + ))); |
|
| 83 | + $cors = $this->client->getBucketCORS(array("Bucket"=>$this->bucket)); |
|
| 84 | + $this->assertEquals(count($cors),2,"bucket cors count "); |
|
| 85 | + $this->client->deleteBucketCORS(array("Bucket"=>$this->bucket)); |
|
| 86 | + $cors = $this->client->getBucketCORS(array("Bucket"=>$this->bucket)); |
|
| 87 | + $this->assertEquals(count($cors),0,"bucket cors count "); |
|
| 88 | + } |
|
| 89 | + public function testCreateBucket(){ |
|
| 90 | + $ex = NULL; |
|
| 91 | + try{ |
|
| 92 | + $this->client->createBucket(array("Bucket"=>$this->bucket)); |
|
| 93 | + }catch(Exception $e){ |
|
| 94 | + $ex = $e; |
|
| 95 | + } |
|
| 96 | + if($ex == NULL||!($ex->errorCode === "BucketAlreadyExists")){ |
|
| 97 | + throw new Exception("create bucket expected BucketAlreadyExists but ".$ex); |
|
| 98 | + } |
|
| 99 | + } |
|
| 100 | + public function testACL(){ |
|
| 101 | + $this->client->setBucketAcl(array("Bucket"=>$this->bucket,"ACL"=>"public-read")); |
|
| 102 | + $acl = $this->client->getBucketAcl(array("Bucket"=>$this->bucket)); |
|
| 103 | + $this->assertEquals($acl,"public-read","bucket acl"); |
|
| 104 | + } |
|
| 105 | + public function testBucketLogging(){ |
|
| 106 | + $this->client->setBucketLogging(array( |
|
| 107 | + "Bucket"=>$this->bucket, |
|
| 108 | + "BucketLogging"=>array( |
|
| 109 | + "Enable"=>TRUE, |
|
| 110 | + "TargetBucket"=>$this->bucket, |
|
| 111 | + "TargetPrefix"=>"X-KSS" |
|
| 112 | 112 | ) |
| 113 | 113 | )); |
| 114 | - $logging = $this->client->getBucketLogging(array("Bucket"=>$this->bucket)); |
|
| 115 | - $this->assertEquals($logging["Enable"],TRUE,"bucket logging enable"); |
|
| 114 | + $logging = $this->client->getBucketLogging(array("Bucket"=>$this->bucket)); |
|
| 115 | + $this->assertEquals($logging["Enable"],TRUE,"bucket logging enable"); |
|
| 116 | 116 | |
| 117 | - $this->client->setBucketLogging(array( |
|
| 118 | - "Bucket"=>$this->bucket, |
|
| 119 | - "BucketLogging"=>array( |
|
| 120 | - "Enable"=>FALSE,//是否开启 |
|
| 121 | - ) |
|
| 122 | - )); |
|
| 123 | - $logging = $this->client->getBucketLogging(array("Bucket"=>$this->bucket)); |
|
| 124 | - $this->assertEquals($logging["Enable"],FALSE,"bucket logging enable"); |
|
| 125 | - } |
|
| 126 | - public function testBucketLocation(){ |
|
| 127 | - $location = $this->client->getBucketLocation(array("Bucket"=>$this->bucket)); |
|
| 128 | - $this->assertEquals($location,"HANGZHOU","bucket location "); |
|
| 129 | - } |
|
| 130 | - public function testPutObjectByContentAndGetObjectContent(){ |
|
| 131 | - $args = array( |
|
| 132 | - "Bucket"=>$this->bucket, |
|
| 133 | - "Key"=>$this->key, |
|
| 134 | - "Content"=>"1234",//要上传的内容 |
|
| 135 | - "ACL"=>"public-read",//可以设置访问权限,合法值,private、public-read |
|
| 136 | - "ObjectMeta"=>array( |
|
| 137 | - "Content-Type"=>"application/xml", |
|
| 138 | - "Content-Length"=>3 |
|
| 117 | + $this->client->setBucketLogging(array( |
|
| 118 | + "Bucket"=>$this->bucket, |
|
| 119 | + "BucketLogging"=>array( |
|
| 120 | + "Enable"=>FALSE,//是否开启 |
|
| 121 | + ) |
|
| 122 | + )); |
|
| 123 | + $logging = $this->client->getBucketLogging(array("Bucket"=>$this->bucket)); |
|
| 124 | + $this->assertEquals($logging["Enable"],FALSE,"bucket logging enable"); |
|
| 125 | + } |
|
| 126 | + public function testBucketLocation(){ |
|
| 127 | + $location = $this->client->getBucketLocation(array("Bucket"=>$this->bucket)); |
|
| 128 | + $this->assertEquals($location,"HANGZHOU","bucket location "); |
|
| 129 | + } |
|
| 130 | + public function testPutObjectByContentAndGetObjectContent(){ |
|
| 131 | + $args = array( |
|
| 132 | + "Bucket"=>$this->bucket, |
|
| 133 | + "Key"=>$this->key, |
|
| 134 | + "Content"=>"1234",//要上传的内容 |
|
| 135 | + "ACL"=>"public-read",//可以设置访问权限,合法值,private、public-read |
|
| 136 | + "ObjectMeta"=>array( |
|
| 137 | + "Content-Type"=>"application/xml", |
|
| 138 | + "Content-Length"=>3 |
|
| 139 | 139 | ), |
| 140 | - "UserMeta"=>array(//可以设置object的用户元数据,需要以x-kss-meta-开头 |
|
| 141 | - "x-kss-meta-test"=>"test" |
|
| 140 | + "UserMeta"=>array(//可以设置object的用户元数据,需要以x-kss-meta-开头 |
|
| 141 | + "x-kss-meta-test"=>"test" |
|
| 142 | 142 | ) |
| 143 | 143 | ); |
| 144 | 144 | $this->client->putObjectByContent($args); |
@@ -156,21 +156,21 @@ discard block |
||
| 156 | 156 | $this->assertEquals($meta["ObjectMeta"]["Content-Type"],"application/xml","Content-Type"); |
| 157 | 157 | $this->assertEquals($meta["ObjectMeta"]["Content-Length"],3,"Content-Length"); |
| 158 | 158 | |
| 159 | - } |
|
| 160 | - public function testPutObjectByFile(){ |
|
| 161 | - $args = array( |
|
| 162 | - "Bucket"=>$this->bucket, |
|
| 163 | - "Key"=>$this->key, |
|
| 164 | - "Content"=>array( |
|
| 165 | - "content"=>$this->cachedir."test_file" |
|
| 166 | - ),//要上传的内容 |
|
| 167 | - "ACL"=>"public-read",//可以设置访问权限,合法值,private、public-read |
|
| 168 | - "ObjectMeta"=>array( |
|
| 169 | - "Content-Type"=>"application/xml", |
|
| 170 | - "Content-Length"=>100 |
|
| 159 | + } |
|
| 160 | + public function testPutObjectByFile(){ |
|
| 161 | + $args = array( |
|
| 162 | + "Bucket"=>$this->bucket, |
|
| 163 | + "Key"=>$this->key, |
|
| 164 | + "Content"=>array( |
|
| 165 | + "content"=>$this->cachedir."test_file" |
|
| 166 | + ),//要上传的内容 |
|
| 167 | + "ACL"=>"public-read",//可以设置访问权限,合法值,private、public-read |
|
| 168 | + "ObjectMeta"=>array( |
|
| 169 | + "Content-Type"=>"application/xml", |
|
| 170 | + "Content-Length"=>100 |
|
| 171 | 171 | ), |
| 172 | - "UserMeta"=>array(//可以设置object的用户元数据,需要以x-kss-meta-开头 |
|
| 173 | - "x-kss-meta-test"=>"test" |
|
| 172 | + "UserMeta"=>array(//可以设置object的用户元数据,需要以x-kss-meta-开头 |
|
| 173 | + "x-kss-meta-test"=>"test" |
|
| 174 | 174 | ) |
| 175 | 175 | ); |
| 176 | 176 | $this->client->putObjectByFile($args); |
@@ -180,47 +180,47 @@ discard block |
||
| 180 | 180 | $this->assertEquals($meta["ObjectMeta"]["Content-Type"],"application/xml","Content-Type"); |
| 181 | 181 | $this->assertEquals($meta["ObjectMeta"]["Content-Length"],100,"Content-Length"); |
| 182 | 182 | $this->assertEquals($this->client->getObjectAcl(array("Bucket"=>$this->bucket,"Key"=>$this->key)),"public-read","object acl "); |
| 183 | - } |
|
| 184 | - public function testObjectAcl(){ |
|
| 185 | - $this->client->putObjectByContent(array("Bucket"=>$this->bucket,"Key"=>$this->key, |
|
| 183 | + } |
|
| 184 | + public function testObjectAcl(){ |
|
| 185 | + $this->client->putObjectByContent(array("Bucket"=>$this->bucket,"Key"=>$this->key, |
|
| 186 | 186 | "Content"=>"1234","ACL"=>"private")); |
| 187 | - $this->assertEquals($this->client->getObjectAcl(array("Bucket"=>$this->bucket,"Key"=>$this->key)),"private","object acl"); |
|
| 188 | - $this->client->setObjectAcl(array("Bucket"=>$this->bucket,"Key"=>$this->key,"ACL"=>"public-read")); |
|
| 189 | - $this->assertEquals($this->client->getObjectAcl(array("Bucket"=>$this->bucket,"Key"=>$this->key)),"public-read","object acl"); |
|
| 190 | - } |
|
| 191 | - public function testDeleteObject(){ |
|
| 192 | - $this->client->putObjectByContent(array("Bucket"=>$this->bucket,"Key"=>$this->key, |
|
| 187 | + $this->assertEquals($this->client->getObjectAcl(array("Bucket"=>$this->bucket,"Key"=>$this->key)),"private","object acl"); |
|
| 188 | + $this->client->setObjectAcl(array("Bucket"=>$this->bucket,"Key"=>$this->key,"ACL"=>"public-read")); |
|
| 189 | + $this->assertEquals($this->client->getObjectAcl(array("Bucket"=>$this->bucket,"Key"=>$this->key)),"public-read","object acl"); |
|
| 190 | + } |
|
| 191 | + public function testDeleteObject(){ |
|
| 192 | + $this->client->putObjectByContent(array("Bucket"=>$this->bucket,"Key"=>$this->key, |
|
| 193 | 193 | "Content"=>"1234")); |
| 194 | - $this->client->deleteObject(array("Bucket"=>$this->bucket,"Key"=>$this->key)); |
|
| 195 | - $this->assertEquals($this->client->objectExists(array("Bucket"=>$this->bucket,"Key"=>$this->key)),FALSE,"object exits"); |
|
| 196 | - } |
|
| 197 | - public function testDeleteObjects(){ |
|
| 198 | - $this->client->putObjectByContent(array("Bucket"=>$this->bucket,"Key"=>$this->key, |
|
| 194 | + $this->client->deleteObject(array("Bucket"=>$this->bucket,"Key"=>$this->key)); |
|
| 195 | + $this->assertEquals($this->client->objectExists(array("Bucket"=>$this->bucket,"Key"=>$this->key)),FALSE,"object exits"); |
|
| 196 | + } |
|
| 197 | + public function testDeleteObjects(){ |
|
| 198 | + $this->client->putObjectByContent(array("Bucket"=>$this->bucket,"Key"=>$this->key, |
|
| 199 | 199 | "Content"=>"1234")); |
| 200 | - $this->client->deleteObjects(array("Bucket"=>$this->bucket,"DeleteKeys"=>array($this->key))); |
|
| 201 | - $this->assertEquals($this->client->objectExists(array("Bucket"=>$this->bucket,"Key"=>$this->key)),FALSE,"object exits"); |
|
| 202 | - } |
|
| 203 | - public function testCopyObject(){ |
|
| 204 | - $this->client->putObjectByContent(array("Bucket"=>$this->bucket,"Key"=>$this->key, |
|
| 200 | + $this->client->deleteObjects(array("Bucket"=>$this->bucket,"DeleteKeys"=>array($this->key))); |
|
| 201 | + $this->assertEquals($this->client->objectExists(array("Bucket"=>$this->bucket,"Key"=>$this->key)),FALSE,"object exits"); |
|
| 202 | + } |
|
| 203 | + public function testCopyObject(){ |
|
| 204 | + $this->client->putObjectByContent(array("Bucket"=>$this->bucket,"Key"=>$this->key, |
|
| 205 | 205 | "Content"=>"1234")); |
| 206 | - $this->client->copyObject(array("Bucket"=>$this->bucket,"Key"=>$this->key_copy,"CopySource"=>array("Bucket"=>$this->bucket,"Key"=>$this->key))); |
|
| 207 | - $this->assertEquals($this->client->objectExists(array("Bucket"=>$this->bucket,"Key"=>$this->key)),TRUE,"object exits"); |
|
| 208 | - $this->assertEquals($this->client->objectExists(array("Bucket"=>$this->bucket,"Key"=>$this->key_copy)),TRUE |
|
| 209 | - ,"object exits"); |
|
| 210 | - } |
|
| 211 | - public function testPutAndGetObject(){ |
|
| 212 | - $args = array( |
|
| 213 | - "Bucket"=>$this->bucket, |
|
| 214 | - "Key"=>$this->key, |
|
| 215 | - "Content"=>array( |
|
| 216 | - "content"=>$this->cachedir."test_file" |
|
| 217 | - ),//要上传的内容 |
|
| 218 | - "ACL"=>"public-read",//可以设置访问权限,合法值,private、public-read |
|
| 219 | - "ObjectMeta"=>array( |
|
| 220 | - "Content-Type"=>"application/xml", |
|
| 206 | + $this->client->copyObject(array("Bucket"=>$this->bucket,"Key"=>$this->key_copy,"CopySource"=>array("Bucket"=>$this->bucket,"Key"=>$this->key))); |
|
| 207 | + $this->assertEquals($this->client->objectExists(array("Bucket"=>$this->bucket,"Key"=>$this->key)),TRUE,"object exits"); |
|
| 208 | + $this->assertEquals($this->client->objectExists(array("Bucket"=>$this->bucket,"Key"=>$this->key_copy)),TRUE |
|
| 209 | + ,"object exits"); |
|
| 210 | + } |
|
| 211 | + public function testPutAndGetObject(){ |
|
| 212 | + $args = array( |
|
| 213 | + "Bucket"=>$this->bucket, |
|
| 214 | + "Key"=>$this->key, |
|
| 215 | + "Content"=>array( |
|
| 216 | + "content"=>$this->cachedir."test_file" |
|
| 217 | + ),//要上传的内容 |
|
| 218 | + "ACL"=>"public-read",//可以设置访问权限,合法值,private、public-read |
|
| 219 | + "ObjectMeta"=>array( |
|
| 220 | + "Content-Type"=>"application/xml", |
|
| 221 | 221 | ), |
| 222 | - "UserMeta"=>array(//可以设置object的用户元数据,需要以x-kss-meta-开头 |
|
| 223 | - "x-kss-meta-test"=>"test" |
|
| 222 | + "UserMeta"=>array(//可以设置object的用户元数据,需要以x-kss-meta-开头 |
|
| 223 | + "x-kss-meta-test"=>"test" |
|
| 224 | 224 | ) |
| 225 | 225 | ); |
| 226 | 226 | $this->client->putObjectByFile($args); |
@@ -229,49 +229,49 @@ discard block |
||
| 229 | 229 | $md5pre = md5_file($this->cachedir."test_file"); |
| 230 | 230 | @unlink($this->cachedir."down"); |
| 231 | 231 | $this->assertEquals($md5,$md5pre,"contentmd5"); |
| 232 | - } |
|
| 233 | - public function testPutAndGetObjectRanges(){ |
|
| 234 | - $args = array( |
|
| 235 | - "Bucket"=>$this->bucket, |
|
| 236 | - "Key"=>$this->key, |
|
| 237 | - "Content"=>array( |
|
| 238 | - "content"=>$this->cachedir."test_file" |
|
| 239 | - ),//要上传的内容 |
|
| 240 | - "ACL"=>"public-read",//可以设置访问权限,合法值,private、public-read |
|
| 241 | - "ObjectMeta"=>array( |
|
| 242 | - "Content-Type"=>"application/xml", |
|
| 232 | + } |
|
| 233 | + public function testPutAndGetObjectRanges(){ |
|
| 234 | + $args = array( |
|
| 235 | + "Bucket"=>$this->bucket, |
|
| 236 | + "Key"=>$this->key, |
|
| 237 | + "Content"=>array( |
|
| 238 | + "content"=>$this->cachedir."test_file" |
|
| 239 | + ),//要上传的内容 |
|
| 240 | + "ACL"=>"public-read",//可以设置访问权限,合法值,private、public-read |
|
| 241 | + "ObjectMeta"=>array( |
|
| 242 | + "Content-Type"=>"application/xml", |
|
| 243 | 243 | ), |
| 244 | - "UserMeta"=>array(//可以设置object的用户元数据,需要以x-kss-meta-开头 |
|
| 245 | - "x-kss-meta-test"=>"test" |
|
| 244 | + "UserMeta"=>array(//可以设置object的用户元数据,需要以x-kss-meta-开头 |
|
| 245 | + "x-kss-meta-test"=>"test" |
|
| 246 | 246 | ) |
| 247 | 247 | ); |
| 248 | 248 | $this->client->putObjectByFile($args); |
| 249 | 249 | rangeGetAndCheckMd5($this->client,$this->bucket,$this->key,$this->cachedir."down",md5_file($this->cachedir."test_file")); |
| 250 | - } |
|
| 251 | - public function testInitAndAbortMultipart(){ |
|
| 252 | - $initResult = $this->client->initMultipartUpload(array("Bucket"=>$this->bucket,"Key"=>$this->key)); |
|
| 253 | - $uid = $initResult["UploadId"]; |
|
| 254 | - $listParts = $this->client->listParts(array("Bucket"=>$this->bucket,"Key"=>$this->key,"Options"=>array("uploadId"=>$uid))); |
|
| 255 | - $this->client->abortMultipartUpload(array("Bucket"=>$this->bucket,"Key"=>$this->key,"Options"=>array("uploadId"=>$uid))); |
|
| 256 | - $ex = NULL; |
|
| 257 | - try{ |
|
| 258 | - $this->client->listParts(array("Bucket"=>$this->bucket,"Key"=>$this->key,"Options"=>array("uploadId"=>$uid))); |
|
| 259 | - }catch(Exception $e){ |
|
| 260 | - $ex = $e; |
|
| 261 | - } |
|
| 262 | - if($ex == NULL||!($ex->errorCode === "NoSuchUpload")){ |
|
| 263 | - throw new Exception("create bucket expected NoSuchUpload but ".$ex); |
|
| 264 | - } |
|
| 265 | - } |
|
| 266 | - public function testMultipartUpload(){ |
|
| 267 | - generateFile(1024*1024,$this->cachedir."multi"); |
|
| 268 | - //初始化分开上传,获取uploadid |
|
| 250 | + } |
|
| 251 | + public function testInitAndAbortMultipart(){ |
|
| 252 | + $initResult = $this->client->initMultipartUpload(array("Bucket"=>$this->bucket,"Key"=>$this->key)); |
|
| 253 | + $uid = $initResult["UploadId"]; |
|
| 254 | + $listParts = $this->client->listParts(array("Bucket"=>$this->bucket,"Key"=>$this->key,"Options"=>array("uploadId"=>$uid))); |
|
| 255 | + $this->client->abortMultipartUpload(array("Bucket"=>$this->bucket,"Key"=>$this->key,"Options"=>array("uploadId"=>$uid))); |
|
| 256 | + $ex = NULL; |
|
| 257 | + try{ |
|
| 258 | + $this->client->listParts(array("Bucket"=>$this->bucket,"Key"=>$this->key,"Options"=>array("uploadId"=>$uid))); |
|
| 259 | + }catch(Exception $e){ |
|
| 260 | + $ex = $e; |
|
| 261 | + } |
|
| 262 | + if($ex == NULL||!($ex->errorCode === "NoSuchUpload")){ |
|
| 263 | + throw new Exception("create bucket expected NoSuchUpload but ".$ex); |
|
| 264 | + } |
|
| 265 | + } |
|
| 266 | + public function testMultipartUpload(){ |
|
| 267 | + generateFile(1024*1024,$this->cachedir."multi"); |
|
| 268 | + //初始化分开上传,获取uploadid |
|
| 269 | 269 | $args = array( |
| 270 | 270 | "Bucket"=>$this->bucket, |
| 271 | 271 | "Key"=>$this->key, |
| 272 | 272 | "ACL"=>"public-read", |
| 273 | 273 | "UserMeta"=>array( |
| 274 | - "x-kss-meta-test"=>"example" |
|
| 274 | + "x-kss-meta-test"=>"example" |
|
| 275 | 275 | ), |
| 276 | 276 | "ObjectMeta"=>array( |
| 277 | 277 | "Content-Type"=>"application/xml" |
@@ -323,21 +323,21 @@ discard block |
||
| 323 | 323 | $this->assertEquals($meta["UserMeta"]["x-kss-meta-test"],"example","x-kss-meta-test"); |
| 324 | 324 | rangeGetAndCheckMd5($this->client,$this->bucket,$this->key,$this->cachedir."down",md5_file($this->cachedir."multi")); |
| 325 | 325 | @unlink($this->cachedir."multi"); |
| 326 | - } |
|
| 327 | - public function testListBucketsPresignedUrl(){ |
|
| 328 | - $url = $this->client->generatePresignedUrl( |
|
| 329 | - array( |
|
| 330 | - "Method"=>"GET", |
|
| 331 | - "Options"=>array("Expires"=>60*10), |
|
| 332 | - "Headers"=>array("Content-Type"=>"text/plain") |
|
| 333 | - )); |
|
| 334 | - $httpRequest = new RequestCore($url); |
|
| 335 | - $httpRequest->set_method("GET"); |
|
| 326 | + } |
|
| 327 | + public function testListBucketsPresignedUrl(){ |
|
| 328 | + $url = $this->client->generatePresignedUrl( |
|
| 329 | + array( |
|
| 330 | + "Method"=>"GET", |
|
| 331 | + "Options"=>array("Expires"=>60*10), |
|
| 332 | + "Headers"=>array("Content-Type"=>"text/plain") |
|
| 333 | + )); |
|
| 334 | + $httpRequest = new RequestCore($url); |
|
| 335 | + $httpRequest->set_method("GET"); |
|
| 336 | 336 | $httpRequest->add_header("Content-Type","text/plain"); |
| 337 | - $httpRequest->send_request(); |
|
| 338 | - $body = $httpRequest->get_response_body (); |
|
| 339 | - $this->assertEquals($httpRequest->get_response_code()." body:".$body,200,"list buckets status code"); |
|
| 340 | - } |
|
| 337 | + $httpRequest->send_request(); |
|
| 338 | + $body = $httpRequest->get_response_body (); |
|
| 339 | + $this->assertEquals($httpRequest->get_response_code()." body:".$body,200,"list buckets status code"); |
|
| 340 | + } |
|
| 341 | 341 | public function testHeadBucketPresignedUrl(){ |
| 342 | 342 | $url = $this->client->generatePresignedUrl( |
| 343 | 343 | array( |
@@ -618,16 +618,16 @@ discard block |
||
| 618 | 618 | "Bucket"=>$this->bucket, |
| 619 | 619 | "Key"=>$this->key, |
| 620 | 620 | "Content"=>"12345",//要上传的内容 |
| 621 | - "ACL"=>"public-read",//可以设置访问权限,合法值,private、public-read |
|
| 622 | - "ObjectMeta"=>array(//设置object的元数据,可以设置"Cache-Control","Content-Disposition","Content-Encoding","Content-Length","Content-MD5","Content-Type","Expires"。当设置了Content-Length时,请勿大于实际长度,如果小于实际长度,将只上传部分内容。 |
|
| 621 | + "ACL"=>"public-read",//可以设置访问权限,合法值,private、public-read |
|
| 622 | + "ObjectMeta"=>array(//设置object的元数据,可以设置"Cache-Control","Content-Disposition","Content-Encoding","Content-Length","Content-MD5","Content-Type","Expires"。当设置了Content-Length时,请勿大于实际长度,如果小于实际长度,将只上传部分内容。 |
|
| 623 | 623 | "Content-Type"=>"binay/ocet-stream" |
| 624 | 624 | ), |
| 625 | 625 | "UserMeta"=>array(//可以设置object的用户元数据,需要以x-kss-meta-开头 |
| 626 | - "x-kss-meta-test"=>"test" |
|
| 626 | + "x-kss-meta-test"=>"test" |
|
| 627 | 627 | ), |
| 628 | 628 | "SSE"=>array( |
| 629 | 629 | "Algm"=>"AES256"//暂时支持AES256 |
| 630 | - ) |
|
| 630 | + ) |
|
| 631 | 631 | ); |
| 632 | 632 | $result = $this->client->putObjectByContent($args); |
| 633 | 633 | $this->assertEquals($result["SSEAlgm"],"AES256"); |
@@ -645,16 +645,16 @@ discard block |
||
| 645 | 645 | "Bucket"=>$this->bucket, |
| 646 | 646 | "Key"=>$this->key, |
| 647 | 647 | "Content"=>"12345",//要上传的内容 |
| 648 | - "ACL"=>"public-read",//可以设置访问权限,合法值,private、public-read |
|
| 649 | - "ObjectMeta"=>array(//设置object的元数据,可以设置"Cache-Control","Content-Disposition","Content-Encoding","Content-Length","Content-MD5","Content-Type","Expires"。当设置了Content-Length时,请勿大于实际长度,如果小于实际长度,将只上传部分内容。 |
|
| 648 | + "ACL"=>"public-read",//可以设置访问权限,合法值,private、public-read |
|
| 649 | + "ObjectMeta"=>array(//设置object的元数据,可以设置"Cache-Control","Content-Disposition","Content-Encoding","Content-Length","Content-MD5","Content-Type","Expires"。当设置了Content-Length时,请勿大于实际长度,如果小于实际长度,将只上传部分内容。 |
|
| 650 | 650 | "Content-Type"=>"binay/ocet-stream" |
| 651 | 651 | ), |
| 652 | 652 | "UserMeta"=>array(//可以设置object的用户元数据,需要以x-kss-meta-开头 |
| 653 | - "x-kss-meta-test"=>"test" |
|
| 653 | + "x-kss-meta-test"=>"test" |
|
| 654 | 654 | ), |
| 655 | 655 | "SSEC"=>array( |
| 656 | 656 | "Key"=>$this->sseckey |
| 657 | - ) |
|
| 657 | + ) |
|
| 658 | 658 | ); |
| 659 | 659 | $result = $this->client->putObjectByContent($args); |
| 660 | 660 | $this->assertEquals($result["SSECAlgm"],"AES256"); |
@@ -665,7 +665,7 @@ discard block |
||
| 665 | 665 | "Key"=>$this->key, |
| 666 | 666 | "SSEC"=>array( |
| 667 | 667 | "Key"=>$this->sseckey |
| 668 | - ) |
|
| 668 | + ) |
|
| 669 | 669 | ); |
| 670 | 670 | $result = $this->client->getObjectMeta($args); |
| 671 | 671 | $this->assertEquals($result["ObjectMeta"]["SSECAlgm"],"AES256"); |
@@ -677,7 +677,7 @@ discard block |
||
| 677 | 677 | "WriteTo"=>$this->cachedir."down", //文件保存路径,必须提供。可以是resource |
| 678 | 678 | "SSEC"=>array( |
| 679 | 679 | "Key"=>$this->sseckey |
| 680 | - ) |
|
| 680 | + ) |
|
| 681 | 681 | ); |
| 682 | 682 | $this->client->getObject($args); |
| 683 | 683 | $this->assertEquals("12345",file_get_contents($this->cachedir."down")); |
@@ -779,16 +779,16 @@ discard block |
||
| 779 | 779 | "Bucket"=>$this->bucket, |
| 780 | 780 | "Key"=>$this->key, |
| 781 | 781 | "Content"=>"12345",//要上传的内容 |
| 782 | - "ACL"=>"public-read",//可以设置访问权限,合法值,private、public-read |
|
| 783 | - "ObjectMeta"=>array(//设置object的元数据,可以设置"Cache-Control","Content-Disposition","Content-Encoding","Content-Length","Content-MD5","Content-Type","Expires"。当设置了Content-Length时,请勿大于实际长度,如果小于实际长度,将只上传部分内容。 |
|
| 782 | + "ACL"=>"public-read",//可以设置访问权限,合法值,private、public-read |
|
| 783 | + "ObjectMeta"=>array(//设置object的元数据,可以设置"Cache-Control","Content-Disposition","Content-Encoding","Content-Length","Content-MD5","Content-Type","Expires"。当设置了Content-Length时,请勿大于实际长度,如果小于实际长度,将只上传部分内容。 |
|
| 784 | 784 | "Content-Type"=>"binay/ocet-stream" |
| 785 | 785 | ), |
| 786 | 786 | "UserMeta"=>array(//可以设置object的用户元数据,需要以x-kss-meta-开头 |
| 787 | - "x-kss-meta-test"=>"test" |
|
| 787 | + "x-kss-meta-test"=>"test" |
|
| 788 | 788 | ), |
| 789 | 789 | "SSEC"=>array( |
| 790 | 790 | "Key"=>$this->sseckey |
| 791 | - ) |
|
| 791 | + ) |
|
| 792 | 792 | ); |
| 793 | 793 | $result = $this->client->putObjectByContent($args); |
| 794 | 794 | |
@@ -799,10 +799,10 @@ discard block |
||
| 799 | 799 | "Bucket"=>$this->bucket, |
| 800 | 800 | "Key"=>$this->key |
| 801 | 801 | ), |
| 802 | - "SSECSource"=>array( |
|
| 802 | + "SSECSource"=>array( |
|
| 803 | 803 | "Key"=>$this->sseckey |
| 804 | 804 | ), |
| 805 | - "SSEC"=>array( |
|
| 805 | + "SSEC"=>array( |
|
| 806 | 806 | "Key"=>$this->sseckey |
| 807 | 807 | ) |
| 808 | 808 | ); |
@@ -821,7 +821,7 @@ discard block |
||
| 821 | 821 | ); |
| 822 | 822 | $this->encryptionClient->putObjectByContent($args); |
| 823 | 823 | rangeGetAndCheckMd5($this->encryptionClient,$this->bucket,$this->key, |
| 824 | - $this->cachedir."down",md5($args["Content"])); |
|
| 824 | + $this->cachedir."down",md5($args["Content"])); |
|
| 825 | 825 | } |
| 826 | 826 | } |
| 827 | 827 | public function testPutObjectByFileAndGetObjectUsingEncyptionMeta(){ |
@@ -835,7 +835,7 @@ discard block |
||
| 835 | 835 | ); |
| 836 | 836 | $this->encryptionClient->putObjectByFile($args); |
| 837 | 837 | rangeGetAndCheckMd5($this->encryptionClient,$this->bucket,$this->key, |
| 838 | - $this->cachedir."down",md5_file($this->cachedir."test_file")); |
|
| 838 | + $this->cachedir."down",md5_file($this->cachedir."test_file")); |
|
| 839 | 839 | } |
| 840 | 840 | public function testMultipartUploadUsingEncyptionMeta(){ |
| 841 | 841 | generateFile(1024*1024,$this->cachedir."multi"); |
@@ -1,53 +1,53 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | function rangeGetAndCheckMd5($client,$bucket,$key,$file,$expectedMd5){ |
| 3 | - $args = array("Bucket"=>$bucket,"Key"=>$key); |
|
| 4 | - $meta = $client->getObjectMeta($args); |
|
| 5 | - $contentlength = $meta["ObjectMeta"]["Content-Length"]; |
|
| 3 | + $args = array("Bucket"=>$bucket,"Key"=>$key); |
|
| 4 | + $meta = $client->getObjectMeta($args); |
|
| 5 | + $contentlength = $meta["ObjectMeta"]["Content-Length"]; |
|
| 6 | 6 | |
| 7 | - $filelist = array(); |
|
| 7 | + $filelist = array(); |
|
| 8 | 8 | |
| 9 | - for($begin = 0;$begin <$contentlength;){ |
|
| 10 | - $index = rand((int)($contentlength/20),(int)($contentlength/4)); |
|
| 11 | - $range = array("start"=>$begin,"end"=>$begin+$index); |
|
| 12 | - $destFile = $file.$begin."-".($begin+$index); |
|
| 13 | - array_push($filelist,$destFile); |
|
| 14 | - $begin += ($index+1); |
|
| 15 | - $args = array( |
|
| 16 | - "Bucket"=>$bucket, |
|
| 17 | - "Key"=>$key, |
|
| 18 | - "Range"=>$range, |
|
| 19 | - "WriteTo"=>$destFile |
|
| 20 | - ); |
|
| 21 | - $client->getObject($args); |
|
| 22 | - } |
|
| 9 | + for($begin = 0;$begin <$contentlength;){ |
|
| 10 | + $index = rand((int)($contentlength/20),(int)($contentlength/4)); |
|
| 11 | + $range = array("start"=>$begin,"end"=>$begin+$index); |
|
| 12 | + $destFile = $file.$begin."-".($begin+$index); |
|
| 13 | + array_push($filelist,$destFile); |
|
| 14 | + $begin += ($index+1); |
|
| 15 | + $args = array( |
|
| 16 | + "Bucket"=>$bucket, |
|
| 17 | + "Key"=>$key, |
|
| 18 | + "Range"=>$range, |
|
| 19 | + "WriteTo"=>$destFile |
|
| 20 | + ); |
|
| 21 | + $client->getObject($args); |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - foreach ($filelist as $key => $value) { |
|
| 25 | - $handle = fopen($value,"r"); |
|
| 26 | - $size = filesize($value); |
|
| 27 | - if($size > 0){ |
|
| 28 | - $content = fread($handle,$size); |
|
| 29 | - file_put_contents($file,$content,FILE_APPEND); |
|
| 30 | - } |
|
| 31 | - fclose($handle); |
|
| 32 | - //@unlink($value); |
|
| 33 | - } |
|
| 34 | - $md5 = md5_file($file); |
|
| 35 | - //@unlink($file); |
|
| 36 | - if($md5 != $expectedMd5) |
|
| 37 | - throw new Exception("file md5 check error expected ".$expectedMd5." ,actual ".$md5, 1); |
|
| 38 | - foreach ($filelist as $key => $value) { |
|
| 39 | - @unlink($value); |
|
| 40 | - } |
|
| 41 | - @unlink($file); |
|
| 24 | + foreach ($filelist as $key => $value) { |
|
| 25 | + $handle = fopen($value,"r"); |
|
| 26 | + $size = filesize($value); |
|
| 27 | + if($size > 0){ |
|
| 28 | + $content = fread($handle,$size); |
|
| 29 | + file_put_contents($file,$content,FILE_APPEND); |
|
| 30 | + } |
|
| 31 | + fclose($handle); |
|
| 32 | + //@unlink($value); |
|
| 33 | + } |
|
| 34 | + $md5 = md5_file($file); |
|
| 35 | + //@unlink($file); |
|
| 36 | + if($md5 != $expectedMd5) |
|
| 37 | + throw new Exception("file md5 check error expected ".$expectedMd5." ,actual ".$md5, 1); |
|
| 38 | + foreach ($filelist as $key => $value) { |
|
| 39 | + @unlink($value); |
|
| 40 | + } |
|
| 41 | + @unlink($file); |
|
| 42 | 42 | } |
| 43 | 43 | function generateFile($sizeInBytes,$destFile){ |
| 44 | - for($i = 0;$i < $sizeInBytes/10;$i++){ |
|
| 45 | - $randpwd = ""; |
|
| 46 | - for ($j = 0; $j < 10; $j++) |
|
| 47 | - { |
|
| 48 | - $randpwd .= chr(mt_rand(33, 126)); |
|
| 49 | - } |
|
| 50 | - file_put_contents($destFile,$randpwd,FILE_APPEND); |
|
| 51 | - } |
|
| 44 | + for($i = 0;$i < $sizeInBytes/10;$i++){ |
|
| 45 | + $randpwd = ""; |
|
| 46 | + for ($j = 0; $j < 10; $j++) |
|
| 47 | + { |
|
| 48 | + $randpwd .= chr(mt_rand(33, 126)); |
|
| 49 | + } |
|
| 50 | + file_put_contents($destFile,$randpwd,FILE_APPEND); |
|
| 51 | + } |
|
| 52 | 52 | } |
| 53 | 53 | ?> |
| 54 | 54 | \ No newline at end of file |
@@ -1,60 +1,60 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | require_once "../core/Logger.php"; |
| 3 | 3 | class PUnit{ |
| 4 | - function assertEquals($value,$expected,$info = NULL){ |
|
| 5 | - if($value != $expected){ |
|
| 6 | - throw new Exception($info." expected ".$expected." but ".$value); |
|
| 7 | - } |
|
| 8 | - } |
|
| 9 | - function run($torun = NULL){ |
|
| 10 | - $r = new ReflectionClass($this); |
|
| 11 | - foreach($r->getMethods() as $key=>$methodObj){ |
|
| 12 | - if($methodObj->isPrivate()) |
|
| 13 | - $methods[$key]['type'] = 'private'; |
|
| 14 | - elseif($methodObj->isProtected()) |
|
| 15 | - $methods[$key]['type'] = 'protected'; |
|
| 16 | - else |
|
| 17 | - $methods[$key]['type'] = 'public'; |
|
| 18 | - $methods[$key]['name'] = $methodObj->name; |
|
| 19 | - $methods[$key]['class'] = $methodObj->class; |
|
| 20 | - } |
|
| 4 | + function assertEquals($value,$expected,$info = NULL){ |
|
| 5 | + if($value != $expected){ |
|
| 6 | + throw new Exception($info." expected ".$expected." but ".$value); |
|
| 7 | + } |
|
| 8 | + } |
|
| 9 | + function run($torun = NULL){ |
|
| 10 | + $r = new ReflectionClass($this); |
|
| 11 | + foreach($r->getMethods() as $key=>$methodObj){ |
|
| 12 | + if($methodObj->isPrivate()) |
|
| 13 | + $methods[$key]['type'] = 'private'; |
|
| 14 | + elseif($methodObj->isProtected()) |
|
| 15 | + $methods[$key]['type'] = 'protected'; |
|
| 16 | + else |
|
| 17 | + $methods[$key]['type'] = 'public'; |
|
| 18 | + $methods[$key]['name'] = $methodObj->name; |
|
| 19 | + $methods[$key]['class'] = $methodObj->class; |
|
| 20 | + } |
|
| 21 | 21 | $before = NULL; |
| 22 | 22 | $after = NULL; |
| 23 | 23 | foreach ($methods as $method) { |
| 24 | - if($method["class"] != "PUnit"&&$method["name"] == "before"){ |
|
| 24 | + if($method["class"] != "PUnit"&&$method["name"] == "before"){ |
|
| 25 | 25 | $before = $method; |
| 26 | - } |
|
| 27 | - if($method["class"] != "PUnit"&&$method["name"] == "after"){ |
|
| 26 | + } |
|
| 27 | + if($method["class"] != "PUnit"&&$method["name"] == "after"){ |
|
| 28 | 28 | $after = $method; |
| 29 | - } |
|
| 29 | + } |
|
| 30 | 30 | } |
| 31 | - $error = array(); |
|
| 32 | - $success = array(); |
|
| 33 | - foreach ($methods as $method) { |
|
| 34 | - if($method["class"] != "PUnit"&&substr($method["name"],0,4) == "test"){ |
|
| 31 | + $error = array(); |
|
| 32 | + $success = array(); |
|
| 33 | + foreach ($methods as $method) { |
|
| 34 | + if($method["class"] != "PUnit"&&substr($method["name"],0,4) == "test"){ |
|
| 35 | 35 | if($torun !== NULL){ |
| 36 | - if(!in_array($method["name"],$torun)) |
|
| 36 | + if(!in_array($method["name"],$torun)) |
|
| 37 | 37 | continue; |
| 38 | 38 | } |
| 39 | - try{ |
|
| 40 | - if($method["type"] == "public"){ |
|
| 39 | + try{ |
|
| 40 | + if($method["type"] == "public"){ |
|
| 41 | 41 | $log = new Logger(); |
| 42 | 42 | $log->info("Run unit --->".$method["name"]); |
| 43 | 43 | if($before!=NULL) |
| 44 | - $this->$before["name"](); |
|
| 45 | - $this->$method["name"](); |
|
| 46 | - array_push($success,$method["name"]); |
|
| 47 | - } |
|
| 48 | - }catch(Exception $e){ |
|
| 49 | - $error[$method["name"]]="".$e; |
|
| 50 | - } |
|
| 51 | - } |
|
| 52 | - } |
|
| 53 | - echo "\r\nPHP Unit-----------error"."\r\n"; |
|
| 54 | - print_r($error); |
|
| 44 | + $this->$before["name"](); |
|
| 45 | + $this->$method["name"](); |
|
| 46 | + array_push($success,$method["name"]); |
|
| 47 | + } |
|
| 48 | + }catch(Exception $e){ |
|
| 49 | + $error[$method["name"]]="".$e; |
|
| 50 | + } |
|
| 51 | + } |
|
| 52 | + } |
|
| 53 | + echo "\r\nPHP Unit-----------error"."\r\n"; |
|
| 54 | + print_r($error); |
|
| 55 | 55 | echo "PHP Unit-----------result"."\r\n"; |
| 56 | 56 | echo "total:".(count($success)+count($error)).",success:".count($success).",error:".count($error)."\r\n"; |
| 57 | 57 | |
| 58 | - } |
|
| 58 | + } |
|
| 59 | 59 | } |
| 60 | 60 | ?> |
| 61 | 61 | \ No newline at end of file |
@@ -1,27 +1,27 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | class DingTalkClient |
| 3 | 3 | { |
| 4 | - /**@Author chaohui.zch copy from TopClient and modify 2016-12-14 **/ |
|
| 4 | + /**@Author chaohui.zch copy from TopClient and modify 2016-12-14 **/ |
|
| 5 | 5 | |
| 6 | 6 | /**@Author chaohui.zch modify $gatewayUrl 2017-07-18 **/ |
| 7 | 7 | public $gatewayUrl = "https://eco.taobao.com/router/rest"; |
| 8 | 8 | |
| 9 | - public $format = "xml"; |
|
| 9 | + public $format = "xml"; |
|
| 10 | 10 | |
| 11 | - public $connectTimeout; |
|
| 11 | + public $connectTimeout; |
|
| 12 | 12 | |
| 13 | - public $readTimeout; |
|
| 13 | + public $readTimeout; |
|
| 14 | 14 | |
| 15 | 15 | public $apiCallType; |
| 16 | 16 | |
| 17 | 17 | public $httpMethod; |
| 18 | 18 | |
| 19 | - /** 是否打开入参check**/ |
|
| 20 | - public $checkRequest = true; |
|
| 19 | + /** 是否打开入参check**/ |
|
| 20 | + public $checkRequest = true; |
|
| 21 | 21 | |
| 22 | - protected $apiVersion = "2.0"; |
|
| 22 | + protected $apiVersion = "2.0"; |
|
| 23 | 23 | |
| 24 | - protected $sdkVersion = "dingtalk-sdk-php-20161214"; |
|
| 24 | + protected $sdkVersion = "dingtalk-sdk-php-20161214"; |
|
| 25 | 25 | |
| 26 | 26 | public function __construct($apiCallType = null, $httpMethod = null, $format = "xml"){ |
| 27 | 27 | $this->apiCallType = $apiCallType; |
@@ -29,80 +29,80 @@ discard block |
||
| 29 | 29 | $this->format = $format; |
| 30 | 30 | } |
| 31 | 31 | |
| 32 | - public function curl($url, $postFields = null) |
|
| 33 | - { |
|
| 34 | - $ch = curl_init(); |
|
| 35 | - curl_setopt($ch, CURLOPT_URL, $url); |
|
| 36 | - curl_setopt($ch, CURLOPT_FAILONERROR, false); |
|
| 37 | - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
| 38 | - if ($this->readTimeout) { |
|
| 39 | - curl_setopt($ch, CURLOPT_TIMEOUT, $this->readTimeout); |
|
| 40 | - } |
|
| 41 | - if ($this->connectTimeout) { |
|
| 42 | - curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout); |
|
| 43 | - } |
|
| 44 | - curl_setopt ( $ch, CURLOPT_USERAGENT, "dingtalk-sdk-php" ); |
|
| 45 | - //https 请求 |
|
| 46 | - if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) { |
|
| 47 | - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
|
| 48 | - curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - if (is_array($postFields) && 0 < count($postFields)) |
|
| 52 | - { |
|
| 53 | - $postBodyString = ""; |
|
| 54 | - $postMultipart = false; |
|
| 55 | - foreach ($postFields as $k => $v) |
|
| 56 | - { |
|
| 57 | - if("@" != substr($v, 0, 1))//判断是不是文件上传 |
|
| 58 | - { |
|
| 59 | - $postBodyString .= "$k=" . urlencode($v) . "&"; |
|
| 60 | - } |
|
| 61 | - else//文件上传用multipart/form-data,否则用www-form-urlencoded |
|
| 62 | - { |
|
| 63 | - $postMultipart = true; |
|
| 64 | - if(class_exists('\CURLFile')){ |
|
| 65 | - $postFields[$k] = new \CURLFile(substr($v, 1)); |
|
| 66 | - } |
|
| 67 | - } |
|
| 68 | - } |
|
| 69 | - unset($k, $v); |
|
| 70 | - curl_setopt($ch, CURLOPT_POST, true); |
|
| 71 | - if ($postMultipart) |
|
| 72 | - { |
|
| 73 | - if (class_exists('\CURLFile')) { |
|
| 74 | - curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true); |
|
| 75 | - } else { |
|
| 76 | - if (defined('CURLOPT_SAFE_UPLOAD')) { |
|
| 77 | - curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false); |
|
| 78 | - } |
|
| 79 | - } |
|
| 80 | - curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); |
|
| 81 | - } |
|
| 82 | - else |
|
| 83 | - { |
|
| 84 | - $header = array("content-type: application/x-www-form-urlencoded; charset=UTF-8"); |
|
| 85 | - curl_setopt($ch,CURLOPT_HTTPHEADER,$header); |
|
| 86 | - curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1)); |
|
| 87 | - } |
|
| 88 | - } |
|
| 89 | - $reponse = curl_exec($ch); |
|
| 32 | + public function curl($url, $postFields = null) |
|
| 33 | + { |
|
| 34 | + $ch = curl_init(); |
|
| 35 | + curl_setopt($ch, CURLOPT_URL, $url); |
|
| 36 | + curl_setopt($ch, CURLOPT_FAILONERROR, false); |
|
| 37 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
| 38 | + if ($this->readTimeout) { |
|
| 39 | + curl_setopt($ch, CURLOPT_TIMEOUT, $this->readTimeout); |
|
| 40 | + } |
|
| 41 | + if ($this->connectTimeout) { |
|
| 42 | + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout); |
|
| 43 | + } |
|
| 44 | + curl_setopt ( $ch, CURLOPT_USERAGENT, "dingtalk-sdk-php" ); |
|
| 45 | + //https 请求 |
|
| 46 | + if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) { |
|
| 47 | + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
|
| 48 | + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + if (is_array($postFields) && 0 < count($postFields)) |
|
| 52 | + { |
|
| 53 | + $postBodyString = ""; |
|
| 54 | + $postMultipart = false; |
|
| 55 | + foreach ($postFields as $k => $v) |
|
| 56 | + { |
|
| 57 | + if("@" != substr($v, 0, 1))//判断是不是文件上传 |
|
| 58 | + { |
|
| 59 | + $postBodyString .= "$k=" . urlencode($v) . "&"; |
|
| 60 | + } |
|
| 61 | + else//文件上传用multipart/form-data,否则用www-form-urlencoded |
|
| 62 | + { |
|
| 63 | + $postMultipart = true; |
|
| 64 | + if(class_exists('\CURLFile')){ |
|
| 65 | + $postFields[$k] = new \CURLFile(substr($v, 1)); |
|
| 66 | + } |
|
| 67 | + } |
|
| 68 | + } |
|
| 69 | + unset($k, $v); |
|
| 70 | + curl_setopt($ch, CURLOPT_POST, true); |
|
| 71 | + if ($postMultipart) |
|
| 72 | + { |
|
| 73 | + if (class_exists('\CURLFile')) { |
|
| 74 | + curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true); |
|
| 75 | + } else { |
|
| 76 | + if (defined('CURLOPT_SAFE_UPLOAD')) { |
|
| 77 | + curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false); |
|
| 78 | + } |
|
| 79 | + } |
|
| 80 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); |
|
| 81 | + } |
|
| 82 | + else |
|
| 83 | + { |
|
| 84 | + $header = array("content-type: application/x-www-form-urlencoded; charset=UTF-8"); |
|
| 85 | + curl_setopt($ch,CURLOPT_HTTPHEADER,$header); |
|
| 86 | + curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1)); |
|
| 87 | + } |
|
| 88 | + } |
|
| 89 | + $reponse = curl_exec($ch); |
|
| 90 | 90 | |
| 91 | - if (curl_errno($ch)) |
|
| 92 | - { |
|
| 93 | - throw new Exception(curl_error($ch),0); |
|
| 94 | - } |
|
| 95 | - else |
|
| 96 | - { |
|
| 97 | - $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
|
| 98 | - if (200 !== $httpStatusCode) |
|
| 99 | - { |
|
| 100 | - throw new Exception($reponse,$httpStatusCode); |
|
| 101 | - } |
|
| 102 | - } |
|
| 103 | - curl_close($ch); |
|
| 104 | - return $reponse; |
|
| 105 | - } |
|
| 91 | + if (curl_errno($ch)) |
|
| 92 | + { |
|
| 93 | + throw new Exception(curl_error($ch),0); |
|
| 94 | + } |
|
| 95 | + else |
|
| 96 | + { |
|
| 97 | + $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
|
| 98 | + if (200 !== $httpStatusCode) |
|
| 99 | + { |
|
| 100 | + throw new Exception($reponse,$httpStatusCode); |
|
| 101 | + } |
|
| 102 | + } |
|
| 103 | + curl_close($ch); |
|
| 104 | + return $reponse; |
|
| 105 | + } |
|
| 106 | 106 | |
| 107 | 107 | public function curl_get($url,$apiFields = null) |
| 108 | 108 | { |
@@ -234,97 +234,97 @@ discard block |
||
| 234 | 234 | return $reponse; |
| 235 | 235 | } |
| 236 | 236 | |
| 237 | - public function curl_with_memory_file($url, $postFields = null, $fileFields = null) |
|
| 238 | - { |
|
| 239 | - $ch = curl_init(); |
|
| 240 | - curl_setopt($ch, CURLOPT_URL, $url); |
|
| 241 | - curl_setopt($ch, CURLOPT_FAILONERROR, false); |
|
| 242 | - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
| 243 | - if ($this->readTimeout) { |
|
| 244 | - curl_setopt($ch, CURLOPT_TIMEOUT, $this->readTimeout); |
|
| 245 | - } |
|
| 246 | - if ($this->connectTimeout) { |
|
| 247 | - curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout); |
|
| 248 | - } |
|
| 249 | - curl_setopt ( $ch, CURLOPT_USERAGENT, "dingtalk-sdk-php" ); |
|
| 250 | - //https 请求 |
|
| 251 | - if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) { |
|
| 252 | - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
|
| 253 | - curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); |
|
| 254 | - } |
|
| 255 | - //生成分隔符 |
|
| 256 | - $delimiter = '-------------' . uniqid(); |
|
| 257 | - //先将post的普通数据生成主体字符串 |
|
| 258 | - $data = ''; |
|
| 259 | - if($postFields != null){ |
|
| 260 | - foreach ($postFields as $name => $content) { |
|
| 261 | - $data .= "--" . $delimiter . "\r\n"; |
|
| 262 | - $data .= 'Content-Disposition: form-data; name="' . $name . '"'; |
|
| 263 | - //multipart/form-data 不需要urlencode,参见 http:stackoverflow.com/questions/6603928/should-i-url-encode-post-data |
|
| 264 | - $data .= "\r\n\r\n" . $content . "\r\n"; |
|
| 265 | - } |
|
| 266 | - unset($name,$content); |
|
| 267 | - } |
|
| 268 | - |
|
| 269 | - //将上传的文件生成主体字符串 |
|
| 270 | - if($fileFields != null){ |
|
| 271 | - foreach ($fileFields as $name => $file) { |
|
| 272 | - $data .= "--" . $delimiter . "\r\n"; |
|
| 273 | - $data .= 'Content-Disposition: form-data; name="' . $name . '"; filename="' . $file['filename'] . "\" \r\n"; |
|
| 274 | - $data .= 'Content-Type: ' . $file['type'] . "\r\n\r\n";//多了个文档类型 |
|
| 275 | - |
|
| 276 | - $data .= $file['content'] . "\r\n"; |
|
| 277 | - } |
|
| 278 | - unset($name,$file); |
|
| 279 | - } |
|
| 280 | - //主体结束的分隔符 |
|
| 281 | - $data .= "--" . $delimiter . "--"; |
|
| 282 | - |
|
| 283 | - curl_setopt($ch, CURLOPT_POST, true); |
|
| 284 | - curl_setopt($ch, CURLOPT_HTTPHEADER , array( |
|
| 285 | - 'Content-Type: multipart/form-data; boundary=' . $delimiter, |
|
| 286 | - 'Content-Length: ' . strlen($data)) |
|
| 287 | - ); |
|
| 288 | - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
|
| 289 | - curl_setopt($ch, CURLOPT_POSTFIELDS, $data); |
|
| 290 | - |
|
| 291 | - $reponse = curl_exec($ch); |
|
| 292 | - unset($data); |
|
| 293 | - |
|
| 294 | - if (curl_errno($ch)) |
|
| 295 | - { |
|
| 296 | - throw new Exception(curl_error($ch),0); |
|
| 297 | - } |
|
| 298 | - else |
|
| 299 | - { |
|
| 300 | - $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
|
| 301 | - if (200 !== $httpStatusCode) |
|
| 302 | - { |
|
| 303 | - throw new Exception($reponse,$httpStatusCode); |
|
| 304 | - } |
|
| 305 | - } |
|
| 306 | - curl_close($ch); |
|
| 307 | - return $reponse; |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - protected function logCommunicationError($apiName, $requestUrl, $errorCode, $responseTxt) |
|
| 311 | - { |
|
| 312 | - $localIp = isset($_SERVER["SERVER_ADDR"]) ? $_SERVER["SERVER_ADDR"] : "CLI"; |
|
| 313 | - $logger = new TopLogger; |
|
| 314 | - $logger->conf["log_file"] = rtrim(TOP_SDK_WORK_DIR, '\\/') . '/' . "logs/top_comm_err_" . "_" . date("Y-m-d") . ".log"; |
|
| 315 | - $logger->conf["separator"] = "^_^"; |
|
| 316 | - $logData = array( |
|
| 317 | - date("Y-m-d H:i:s"), |
|
| 318 | - $apiName, |
|
| 319 | - $localIp, |
|
| 320 | - PHP_OS, |
|
| 321 | - $this->sdkVersion, |
|
| 322 | - $requestUrl, |
|
| 323 | - $errorCode, |
|
| 324 | - str_replace("\n","",$responseTxt) |
|
| 325 | - ); |
|
| 326 | - $logger->log($logData); |
|
| 327 | - } |
|
| 237 | + public function curl_with_memory_file($url, $postFields = null, $fileFields = null) |
|
| 238 | + { |
|
| 239 | + $ch = curl_init(); |
|
| 240 | + curl_setopt($ch, CURLOPT_URL, $url); |
|
| 241 | + curl_setopt($ch, CURLOPT_FAILONERROR, false); |
|
| 242 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
| 243 | + if ($this->readTimeout) { |
|
| 244 | + curl_setopt($ch, CURLOPT_TIMEOUT, $this->readTimeout); |
|
| 245 | + } |
|
| 246 | + if ($this->connectTimeout) { |
|
| 247 | + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout); |
|
| 248 | + } |
|
| 249 | + curl_setopt ( $ch, CURLOPT_USERAGENT, "dingtalk-sdk-php" ); |
|
| 250 | + //https 请求 |
|
| 251 | + if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) { |
|
| 252 | + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
|
| 253 | + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); |
|
| 254 | + } |
|
| 255 | + //生成分隔符 |
|
| 256 | + $delimiter = '-------------' . uniqid(); |
|
| 257 | + //先将post的普通数据生成主体字符串 |
|
| 258 | + $data = ''; |
|
| 259 | + if($postFields != null){ |
|
| 260 | + foreach ($postFields as $name => $content) { |
|
| 261 | + $data .= "--" . $delimiter . "\r\n"; |
|
| 262 | + $data .= 'Content-Disposition: form-data; name="' . $name . '"'; |
|
| 263 | + //multipart/form-data 不需要urlencode,参见 http:stackoverflow.com/questions/6603928/should-i-url-encode-post-data |
|
| 264 | + $data .= "\r\n\r\n" . $content . "\r\n"; |
|
| 265 | + } |
|
| 266 | + unset($name,$content); |
|
| 267 | + } |
|
| 268 | + |
|
| 269 | + //将上传的文件生成主体字符串 |
|
| 270 | + if($fileFields != null){ |
|
| 271 | + foreach ($fileFields as $name => $file) { |
|
| 272 | + $data .= "--" . $delimiter . "\r\n"; |
|
| 273 | + $data .= 'Content-Disposition: form-data; name="' . $name . '"; filename="' . $file['filename'] . "\" \r\n"; |
|
| 274 | + $data .= 'Content-Type: ' . $file['type'] . "\r\n\r\n";//多了个文档类型 |
|
| 275 | + |
|
| 276 | + $data .= $file['content'] . "\r\n"; |
|
| 277 | + } |
|
| 278 | + unset($name,$file); |
|
| 279 | + } |
|
| 280 | + //主体结束的分隔符 |
|
| 281 | + $data .= "--" . $delimiter . "--"; |
|
| 282 | + |
|
| 283 | + curl_setopt($ch, CURLOPT_POST, true); |
|
| 284 | + curl_setopt($ch, CURLOPT_HTTPHEADER , array( |
|
| 285 | + 'Content-Type: multipart/form-data; boundary=' . $delimiter, |
|
| 286 | + 'Content-Length: ' . strlen($data)) |
|
| 287 | + ); |
|
| 288 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
|
| 289 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $data); |
|
| 290 | + |
|
| 291 | + $reponse = curl_exec($ch); |
|
| 292 | + unset($data); |
|
| 293 | + |
|
| 294 | + if (curl_errno($ch)) |
|
| 295 | + { |
|
| 296 | + throw new Exception(curl_error($ch),0); |
|
| 297 | + } |
|
| 298 | + else |
|
| 299 | + { |
|
| 300 | + $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
|
| 301 | + if (200 !== $httpStatusCode) |
|
| 302 | + { |
|
| 303 | + throw new Exception($reponse,$httpStatusCode); |
|
| 304 | + } |
|
| 305 | + } |
|
| 306 | + curl_close($ch); |
|
| 307 | + return $reponse; |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + protected function logCommunicationError($apiName, $requestUrl, $errorCode, $responseTxt) |
|
| 311 | + { |
|
| 312 | + $localIp = isset($_SERVER["SERVER_ADDR"]) ? $_SERVER["SERVER_ADDR"] : "CLI"; |
|
| 313 | + $logger = new TopLogger; |
|
| 314 | + $logger->conf["log_file"] = rtrim(TOP_SDK_WORK_DIR, '\\/') . '/' . "logs/top_comm_err_" . "_" . date("Y-m-d") . ".log"; |
|
| 315 | + $logger->conf["separator"] = "^_^"; |
|
| 316 | + $logData = array( |
|
| 317 | + date("Y-m-d H:i:s"), |
|
| 318 | + $apiName, |
|
| 319 | + $localIp, |
|
| 320 | + PHP_OS, |
|
| 321 | + $this->sdkVersion, |
|
| 322 | + $requestUrl, |
|
| 323 | + $errorCode, |
|
| 324 | + str_replace("\n","",$responseTxt) |
|
| 325 | + ); |
|
| 326 | + $logger->log($logData); |
|
| 327 | + } |
|
| 328 | 328 | |
| 329 | 329 | public function execute($request, $session = null,$bestUrl = null){ |
| 330 | 330 | if(DingTalkConstant::$CALL_TYPE_OAPI == $this->apiCallType){ |
@@ -342,7 +342,7 @@ discard block |
||
| 342 | 342 | return $this->executeWithCorpId($request,$bestUrl, $accessKey, $accessSecret, $suiteTicket, null); |
| 343 | 343 | } |
| 344 | 344 | |
| 345 | - public function executeWithCorpId($request, $bestUrl = null, $accessKey, $accessSecret, $suiteTicket, $corpId) { |
|
| 345 | + public function executeWithCorpId($request, $bestUrl = null, $accessKey, $accessSecret, $suiteTicket, $corpId) { |
|
| 346 | 346 | if(DingTalkConstant::$CALL_TYPE_OAPI == $this->apiCallType){ |
| 347 | 347 | return $this->_executeOapi($request, null, $bestUrl,$accessKey, $accessSecret, $suiteTicket, $corpId); |
| 348 | 348 | }else{ |
@@ -494,160 +494,160 @@ discard block |
||
| 494 | 494 | } |
| 495 | 495 | |
| 496 | 496 | private function _execute($request, $session = null,$bestUrl = null) |
| 497 | - { |
|
| 498 | - $result = new ResultSet(); |
|
| 499 | - if($this->checkRequest) { |
|
| 500 | - try { |
|
| 501 | - $request->check(); |
|
| 502 | - } catch (Exception $e) { |
|
| 503 | - |
|
| 504 | - $result->code = $e->getCode(); |
|
| 505 | - $result->msg = $e->getMessage(); |
|
| 506 | - return $result; |
|
| 507 | - } |
|
| 508 | - } |
|
| 509 | - //组装系统参数 |
|
| 510 | - $sysParams["v"] = $this->apiVersion; |
|
| 511 | - $sysParams["format"] = $this->format; |
|
| 512 | - $sysParams["method"] = $request->getApiMethodName(); |
|
| 513 | - $sysParams["timestamp"] = date("Y-m-d H:i:s"); |
|
| 514 | - if (null != $session) |
|
| 515 | - { |
|
| 516 | - $sysParams["session"] = $session; |
|
| 517 | - } |
|
| 518 | - $apiParams = array(); |
|
| 519 | - //获取业务参数 |
|
| 520 | - $apiParams = $request->getApiParas(); |
|
| 521 | - |
|
| 522 | - |
|
| 523 | - //系统参数放入GET请求串 |
|
| 524 | - if($bestUrl){ |
|
| 497 | + { |
|
| 498 | + $result = new ResultSet(); |
|
| 499 | + if($this->checkRequest) { |
|
| 500 | + try { |
|
| 501 | + $request->check(); |
|
| 502 | + } catch (Exception $e) { |
|
| 503 | + |
|
| 504 | + $result->code = $e->getCode(); |
|
| 505 | + $result->msg = $e->getMessage(); |
|
| 506 | + return $result; |
|
| 507 | + } |
|
| 508 | + } |
|
| 509 | + //组装系统参数 |
|
| 510 | + $sysParams["v"] = $this->apiVersion; |
|
| 511 | + $sysParams["format"] = $this->format; |
|
| 512 | + $sysParams["method"] = $request->getApiMethodName(); |
|
| 513 | + $sysParams["timestamp"] = date("Y-m-d H:i:s"); |
|
| 514 | + if (null != $session) |
|
| 515 | + { |
|
| 516 | + $sysParams["session"] = $session; |
|
| 517 | + } |
|
| 518 | + $apiParams = array(); |
|
| 519 | + //获取业务参数 |
|
| 520 | + $apiParams = $request->getApiParas(); |
|
| 521 | + |
|
| 522 | + |
|
| 523 | + //系统参数放入GET请求串 |
|
| 524 | + if($bestUrl){ |
|
| 525 | 525 | if(strpos($bestUrl,'?') === false){ |
| 526 | 526 | $requestUrl = $bestUrl."?"; |
| 527 | 527 | }else{ |
| 528 | 528 | $requestUrl = $bestUrl; |
| 529 | 529 | } |
| 530 | - $sysParams["partner_id"] = $this->getClusterTag(); |
|
| 531 | - }else{ |
|
| 532 | - $requestUrl = $this->gatewayUrl."?"; |
|
| 533 | - $sysParams["partner_id"] = $this->sdkVersion; |
|
| 534 | - } |
|
| 535 | - |
|
| 536 | - foreach ($sysParams as $sysParamKey => $sysParamValue) |
|
| 537 | - { |
|
| 538 | - // if(strcmp($sysParamKey,"timestamp") != 0) |
|
| 539 | - $requestUrl .= "$sysParamKey=" . urlencode($sysParamValue) . "&"; |
|
| 540 | - } |
|
| 541 | - |
|
| 542 | - $fileFields = array(); |
|
| 543 | - foreach ($apiParams as $key => $value) { |
|
| 544 | - if(is_array($value) && array_key_exists('type',$value) && array_key_exists('content',$value) ){ |
|
| 545 | - $value['name'] = $key; |
|
| 546 | - $fileFields[$key] = $value; |
|
| 547 | - unset($apiParams[$key]); |
|
| 548 | - } |
|
| 549 | - } |
|
| 550 | - |
|
| 551 | - // $requestUrl .= "timestamp=" . urlencode($sysParams["timestamp"]) . "&"; |
|
| 552 | - $requestUrl = substr($requestUrl, 0, -1); |
|
| 553 | - |
|
| 554 | - //发起HTTP请求 |
|
| 555 | - try |
|
| 556 | - { |
|
| 557 | - if(count($fileFields) > 0){ |
|
| 558 | - $resp = $this->curl_with_memory_file($requestUrl, $apiParams, $fileFields); |
|
| 559 | - }else{ |
|
| 560 | - $resp = $this->curl($requestUrl, $apiParams); |
|
| 561 | - } |
|
| 562 | - } |
|
| 563 | - catch (Exception $e) |
|
| 564 | - { |
|
| 565 | - $this->logCommunicationError($sysParams["method"],$requestUrl,"HTTP_ERROR_" . $e->getCode(),$e->getMessage()); |
|
| 566 | - $result->code = $e->getCode(); |
|
| 567 | - $result->msg = $e->getMessage(); |
|
| 568 | - return $result; |
|
| 569 | - } |
|
| 570 | - |
|
| 571 | - unset($apiParams); |
|
| 572 | - unset($fileFields); |
|
| 573 | - //解析TOP返回结果 |
|
| 574 | - $respWellFormed = false; |
|
| 575 | - if ("json" == $this->format) |
|
| 576 | - { |
|
| 577 | - $respObject = json_decode($resp); |
|
| 578 | - if (null !== $respObject) |
|
| 579 | - { |
|
| 580 | - $respWellFormed = true; |
|
| 581 | - foreach ($respObject as $propKey => $propValue) |
|
| 582 | - { |
|
| 583 | - $respObject = $propValue; |
|
| 584 | - } |
|
| 585 | - } |
|
| 586 | - } |
|
| 587 | - else if("xml" == $this->format) |
|
| 588 | - { |
|
| 589 | - $respObject = @simplexml_load_string($resp); |
|
| 590 | - if (false !== $respObject) |
|
| 591 | - { |
|
| 592 | - $respWellFormed = true; |
|
| 593 | - } |
|
| 594 | - } |
|
| 595 | - |
|
| 596 | - //返回的HTTP文本不是标准JSON或者XML,记下错误日志 |
|
| 597 | - if (false === $respWellFormed) |
|
| 598 | - { |
|
| 599 | - $this->logCommunicationError($sysParams["method"],$requestUrl,"HTTP_RESPONSE_NOT_WELL_FORMED",$resp); |
|
| 600 | - $result->code = 0; |
|
| 601 | - $result->msg = "HTTP_RESPONSE_NOT_WELL_FORMED"; |
|
| 602 | - return $result; |
|
| 603 | - } |
|
| 604 | - |
|
| 605 | - //如果TOP返回了错误码,记录到业务错误日志中 |
|
| 606 | - if (isset($respObject->code)) |
|
| 607 | - { |
|
| 608 | - $logger = new TopLogger; |
|
| 609 | - $logger->conf["log_file"] = rtrim(TOP_SDK_WORK_DIR, '\\/') . '/' . "logs/top_biz_err_" . "_" . date("Y-m-d") . ".log"; |
|
| 610 | - $logger->log(array( |
|
| 611 | - date("Y-m-d H:i:s"), |
|
| 612 | - $resp |
|
| 613 | - )); |
|
| 614 | - } |
|
| 615 | - return $respObject; |
|
| 616 | - } |
|
| 617 | - |
|
| 618 | - public function exec($paramsArray) |
|
| 619 | - { |
|
| 620 | - if (!isset($paramsArray["method"])) |
|
| 621 | - { |
|
| 622 | - trigger_error("No api name passed"); |
|
| 623 | - } |
|
| 624 | - $inflector = new LtInflector; |
|
| 625 | - $inflector->conf["separator"] = "."; |
|
| 626 | - $requestClassName = ucfirst($inflector->camelize(substr($paramsArray["method"], 7))) . "Request"; |
|
| 627 | - if (!class_exists($requestClassName)) |
|
| 628 | - { |
|
| 629 | - trigger_error("No such dingtalk-api: " . $paramsArray["method"]); |
|
| 630 | - } |
|
| 631 | - |
|
| 632 | - $session = isset($paramsArray["session"]) ? $paramsArray["session"] : null; |
|
| 633 | - |
|
| 634 | - $req = new $requestClassName; |
|
| 635 | - foreach($paramsArray as $paraKey => $paraValue) |
|
| 636 | - { |
|
| 637 | - $inflector->conf["separator"] = "_"; |
|
| 638 | - $setterMethodName = $inflector->camelize($paraKey); |
|
| 639 | - $inflector->conf["separator"] = "."; |
|
| 640 | - $setterMethodName = "set" . $inflector->camelize($setterMethodName); |
|
| 641 | - if (method_exists($req, $setterMethodName)) |
|
| 642 | - { |
|
| 643 | - $req->$setterMethodName($paraValue); |
|
| 644 | - } |
|
| 645 | - } |
|
| 646 | - return $this->execute($req, $session); |
|
| 647 | - } |
|
| 648 | - |
|
| 649 | - private function getClusterTag() |
|
| 530 | + $sysParams["partner_id"] = $this->getClusterTag(); |
|
| 531 | + }else{ |
|
| 532 | + $requestUrl = $this->gatewayUrl."?"; |
|
| 533 | + $sysParams["partner_id"] = $this->sdkVersion; |
|
| 534 | + } |
|
| 535 | + |
|
| 536 | + foreach ($sysParams as $sysParamKey => $sysParamValue) |
|
| 537 | + { |
|
| 538 | + // if(strcmp($sysParamKey,"timestamp") != 0) |
|
| 539 | + $requestUrl .= "$sysParamKey=" . urlencode($sysParamValue) . "&"; |
|
| 540 | + } |
|
| 541 | + |
|
| 542 | + $fileFields = array(); |
|
| 543 | + foreach ($apiParams as $key => $value) { |
|
| 544 | + if(is_array($value) && array_key_exists('type',$value) && array_key_exists('content',$value) ){ |
|
| 545 | + $value['name'] = $key; |
|
| 546 | + $fileFields[$key] = $value; |
|
| 547 | + unset($apiParams[$key]); |
|
| 548 | + } |
|
| 549 | + } |
|
| 550 | + |
|
| 551 | + // $requestUrl .= "timestamp=" . urlencode($sysParams["timestamp"]) . "&"; |
|
| 552 | + $requestUrl = substr($requestUrl, 0, -1); |
|
| 553 | + |
|
| 554 | + //发起HTTP请求 |
|
| 555 | + try |
|
| 556 | + { |
|
| 557 | + if(count($fileFields) > 0){ |
|
| 558 | + $resp = $this->curl_with_memory_file($requestUrl, $apiParams, $fileFields); |
|
| 559 | + }else{ |
|
| 560 | + $resp = $this->curl($requestUrl, $apiParams); |
|
| 561 | + } |
|
| 562 | + } |
|
| 563 | + catch (Exception $e) |
|
| 564 | + { |
|
| 565 | + $this->logCommunicationError($sysParams["method"],$requestUrl,"HTTP_ERROR_" . $e->getCode(),$e->getMessage()); |
|
| 566 | + $result->code = $e->getCode(); |
|
| 567 | + $result->msg = $e->getMessage(); |
|
| 568 | + return $result; |
|
| 569 | + } |
|
| 570 | + |
|
| 571 | + unset($apiParams); |
|
| 572 | + unset($fileFields); |
|
| 573 | + //解析TOP返回结果 |
|
| 574 | + $respWellFormed = false; |
|
| 575 | + if ("json" == $this->format) |
|
| 576 | + { |
|
| 577 | + $respObject = json_decode($resp); |
|
| 578 | + if (null !== $respObject) |
|
| 579 | + { |
|
| 580 | + $respWellFormed = true; |
|
| 581 | + foreach ($respObject as $propKey => $propValue) |
|
| 582 | + { |
|
| 583 | + $respObject = $propValue; |
|
| 584 | + } |
|
| 585 | + } |
|
| 586 | + } |
|
| 587 | + else if("xml" == $this->format) |
|
| 588 | + { |
|
| 589 | + $respObject = @simplexml_load_string($resp); |
|
| 590 | + if (false !== $respObject) |
|
| 591 | + { |
|
| 592 | + $respWellFormed = true; |
|
| 593 | + } |
|
| 594 | + } |
|
| 595 | + |
|
| 596 | + //返回的HTTP文本不是标准JSON或者XML,记下错误日志 |
|
| 597 | + if (false === $respWellFormed) |
|
| 598 | + { |
|
| 599 | + $this->logCommunicationError($sysParams["method"],$requestUrl,"HTTP_RESPONSE_NOT_WELL_FORMED",$resp); |
|
| 600 | + $result->code = 0; |
|
| 601 | + $result->msg = "HTTP_RESPONSE_NOT_WELL_FORMED"; |
|
| 602 | + return $result; |
|
| 603 | + } |
|
| 604 | + |
|
| 605 | + //如果TOP返回了错误码,记录到业务错误日志中 |
|
| 606 | + if (isset($respObject->code)) |
|
| 607 | + { |
|
| 608 | + $logger = new TopLogger; |
|
| 609 | + $logger->conf["log_file"] = rtrim(TOP_SDK_WORK_DIR, '\\/') . '/' . "logs/top_biz_err_" . "_" . date("Y-m-d") . ".log"; |
|
| 610 | + $logger->log(array( |
|
| 611 | + date("Y-m-d H:i:s"), |
|
| 612 | + $resp |
|
| 613 | + )); |
|
| 614 | + } |
|
| 615 | + return $respObject; |
|
| 616 | + } |
|
| 617 | + |
|
| 618 | + public function exec($paramsArray) |
|
| 619 | + { |
|
| 620 | + if (!isset($paramsArray["method"])) |
|
| 621 | + { |
|
| 622 | + trigger_error("No api name passed"); |
|
| 623 | + } |
|
| 624 | + $inflector = new LtInflector; |
|
| 625 | + $inflector->conf["separator"] = "."; |
|
| 626 | + $requestClassName = ucfirst($inflector->camelize(substr($paramsArray["method"], 7))) . "Request"; |
|
| 627 | + if (!class_exists($requestClassName)) |
|
| 628 | + { |
|
| 629 | + trigger_error("No such dingtalk-api: " . $paramsArray["method"]); |
|
| 630 | + } |
|
| 631 | + |
|
| 632 | + $session = isset($paramsArray["session"]) ? $paramsArray["session"] : null; |
|
| 633 | + |
|
| 634 | + $req = new $requestClassName; |
|
| 635 | + foreach($paramsArray as $paraKey => $paraValue) |
|
| 636 | + { |
|
| 637 | + $inflector->conf["separator"] = "_"; |
|
| 638 | + $setterMethodName = $inflector->camelize($paraKey); |
|
| 639 | + $inflector->conf["separator"] = "."; |
|
| 640 | + $setterMethodName = "set" . $inflector->camelize($setterMethodName); |
|
| 641 | + if (method_exists($req, $setterMethodName)) |
|
| 642 | + { |
|
| 643 | + $req->$setterMethodName($paraValue); |
|
| 644 | + } |
|
| 645 | + } |
|
| 646 | + return $this->execute($req, $session); |
|
| 647 | + } |
|
| 648 | + |
|
| 649 | + private function getClusterTag() |
|
| 650 | 650 | { |
| 651 | - return substr($this->sdkVersion,0,11)."-cluster".substr($this->sdkVersion,11); |
|
| 651 | + return substr($this->sdkVersion,0,11)."-cluster".substr($this->sdkVersion,11); |
|
| 652 | 652 | } |
| 653 | 653 | } |
@@ -15,7 +15,7 @@ discard block |
||
| 15 | 15 | */ |
| 16 | 16 | if (!defined("TOP_SDK_WORK_DIR")) |
| 17 | 17 | { |
| 18 | - define("TOP_SDK_WORK_DIR", "/tmp/"); |
|
| 18 | + define("TOP_SDK_WORK_DIR", "/tmp/"); |
|
| 19 | 19 | } |
| 20 | 20 | |
| 21 | 21 | /** |
@@ -25,12 +25,12 @@ discard block |
||
| 25 | 25 | */ |
| 26 | 26 | if (!defined("TOP_SDK_DEV_MODE")) |
| 27 | 27 | { |
| 28 | - define("TOP_SDK_DEV_MODE", true); |
|
| 28 | + define("TOP_SDK_DEV_MODE", true); |
|
| 29 | 29 | } |
| 30 | 30 | |
| 31 | 31 | if (!defined("TOP_AUTOLOADER_PATH")) |
| 32 | 32 | { |
| 33 | - define("TOP_AUTOLOADER_PATH", dirname(__FILE__)); |
|
| 33 | + define("TOP_AUTOLOADER_PATH", dirname(__FILE__)); |
|
| 34 | 34 | } |
| 35 | 35 | |
| 36 | 36 | /** |
@@ -1,384 +1,384 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | class QimenCloudClient |
| 3 | 3 | { |
| 4 | - public $appkey; |
|
| 4 | + public $appkey; |
|
| 5 | 5 | |
| 6 | - public $secretKey; |
|
| 6 | + public $secretKey; |
|
| 7 | 7 | |
| 8 | - public $targetAppkey = ""; |
|
| 9 | - |
|
| 10 | - public $gatewayUrl = null; |
|
| 11 | - |
|
| 12 | - public $format = "xml"; |
|
| 13 | - |
|
| 14 | - public $connectTimeout; |
|
| 15 | - |
|
| 16 | - public $readTimeout; |
|
| 17 | - |
|
| 18 | - /** 是否打开入参check**/ |
|
| 19 | - public $checkRequest = true; |
|
| 20 | - |
|
| 21 | - protected $signMethod = "md5"; |
|
| 22 | - |
|
| 23 | - protected $apiVersion = "2.0"; |
|
| 24 | - |
|
| 25 | - protected $sdkVersion = "top-sdk-php-20151012"; |
|
| 26 | - |
|
| 27 | - public function getAppkey() |
|
| 28 | - { |
|
| 29 | - return $this->appkey; |
|
| 30 | - } |
|
| 31 | - |
|
| 32 | - public function __construct($appkey = "",$secretKey = ""){ |
|
| 33 | - $this->appkey = $appkey; |
|
| 34 | - $this->secretKey = $secretKey ; |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - protected function generateSign($params) |
|
| 38 | - { |
|
| 39 | - ksort($params); |
|
| 40 | - |
|
| 41 | - $stringToBeSigned = $this->secretKey; |
|
| 42 | - foreach ($params as $k => $v) |
|
| 43 | - { |
|
| 44 | - if(!is_array($v) && "@" != substr($v, 0, 1)) |
|
| 45 | - { |
|
| 46 | - $stringToBeSigned .= "$k$v"; |
|
| 47 | - } |
|
| 48 | - } |
|
| 49 | - unset($k, $v); |
|
| 50 | - $stringToBeSigned .= $this->secretKey; |
|
| 51 | - |
|
| 52 | - return strtoupper(md5($stringToBeSigned)); |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - public function curl($url, $postFields = null) |
|
| 56 | - { |
|
| 57 | - $ch = curl_init(); |
|
| 58 | - curl_setopt($ch, CURLOPT_URL, $url); |
|
| 59 | - curl_setopt($ch, CURLOPT_FAILONERROR, false); |
|
| 60 | - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
| 61 | - if ($this->readTimeout) { |
|
| 62 | - curl_setopt($ch, CURLOPT_TIMEOUT, $this->readTimeout); |
|
| 63 | - } |
|
| 64 | - if ($this->connectTimeout) { |
|
| 65 | - curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout); |
|
| 66 | - } |
|
| 67 | - curl_setopt ( $ch, CURLOPT_USERAGENT, "top-sdk-php" ); |
|
| 68 | - //https 请求 |
|
| 69 | - if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) { |
|
| 70 | - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
|
| 71 | - curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - if (is_array($postFields) && 0 < count($postFields)) |
|
| 75 | - { |
|
| 76 | - $postBodyString = ""; |
|
| 77 | - $postMultipart = false; |
|
| 78 | - foreach ($postFields as $k => $v) |
|
| 79 | - { |
|
| 80 | - if("@" != substr($v, 0, 1))//判断是不是文件上传 |
|
| 81 | - { |
|
| 82 | - $postBodyString .= "$k=" . urlencode($v) . "&"; |
|
| 83 | - } |
|
| 84 | - else//文件上传用multipart/form-data,否则用www-form-urlencoded |
|
| 85 | - { |
|
| 86 | - $postMultipart = true; |
|
| 87 | - if(class_exists('\CURLFile')){ |
|
| 88 | - $postFields[$k] = new \CURLFile(substr($v, 1)); |
|
| 89 | - } |
|
| 90 | - } |
|
| 91 | - } |
|
| 92 | - unset($k, $v); |
|
| 93 | - curl_setopt($ch, CURLOPT_POST, true); |
|
| 94 | - if ($postMultipart) |
|
| 95 | - { |
|
| 96 | - if (class_exists('\CURLFile')) { |
|
| 97 | - curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true); |
|
| 98 | - } else { |
|
| 99 | - if (defined('CURLOPT_SAFE_UPLOAD')) { |
|
| 100 | - curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false); |
|
| 101 | - } |
|
| 102 | - } |
|
| 103 | - curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); |
|
| 104 | - } |
|
| 105 | - else |
|
| 106 | - { |
|
| 107 | - $header = array("content-type: application/x-www-form-urlencoded; charset=UTF-8"); |
|
| 108 | - curl_setopt($ch,CURLOPT_HTTPHEADER,$header); |
|
| 109 | - curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1)); |
|
| 110 | - } |
|
| 111 | - } |
|
| 112 | - $reponse = curl_exec($ch); |
|
| 8 | + public $targetAppkey = ""; |
|
| 9 | + |
|
| 10 | + public $gatewayUrl = null; |
|
| 11 | + |
|
| 12 | + public $format = "xml"; |
|
| 13 | + |
|
| 14 | + public $connectTimeout; |
|
| 15 | + |
|
| 16 | + public $readTimeout; |
|
| 17 | + |
|
| 18 | + /** 是否打开入参check**/ |
|
| 19 | + public $checkRequest = true; |
|
| 20 | + |
|
| 21 | + protected $signMethod = "md5"; |
|
| 22 | + |
|
| 23 | + protected $apiVersion = "2.0"; |
|
| 24 | + |
|
| 25 | + protected $sdkVersion = "top-sdk-php-20151012"; |
|
| 26 | + |
|
| 27 | + public function getAppkey() |
|
| 28 | + { |
|
| 29 | + return $this->appkey; |
|
| 30 | + } |
|
| 31 | + |
|
| 32 | + public function __construct($appkey = "",$secretKey = ""){ |
|
| 33 | + $this->appkey = $appkey; |
|
| 34 | + $this->secretKey = $secretKey ; |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + protected function generateSign($params) |
|
| 38 | + { |
|
| 39 | + ksort($params); |
|
| 40 | + |
|
| 41 | + $stringToBeSigned = $this->secretKey; |
|
| 42 | + foreach ($params as $k => $v) |
|
| 43 | + { |
|
| 44 | + if(!is_array($v) && "@" != substr($v, 0, 1)) |
|
| 45 | + { |
|
| 46 | + $stringToBeSigned .= "$k$v"; |
|
| 47 | + } |
|
| 48 | + } |
|
| 49 | + unset($k, $v); |
|
| 50 | + $stringToBeSigned .= $this->secretKey; |
|
| 51 | + |
|
| 52 | + return strtoupper(md5($stringToBeSigned)); |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + public function curl($url, $postFields = null) |
|
| 56 | + { |
|
| 57 | + $ch = curl_init(); |
|
| 58 | + curl_setopt($ch, CURLOPT_URL, $url); |
|
| 59 | + curl_setopt($ch, CURLOPT_FAILONERROR, false); |
|
| 60 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
| 61 | + if ($this->readTimeout) { |
|
| 62 | + curl_setopt($ch, CURLOPT_TIMEOUT, $this->readTimeout); |
|
| 63 | + } |
|
| 64 | + if ($this->connectTimeout) { |
|
| 65 | + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout); |
|
| 66 | + } |
|
| 67 | + curl_setopt ( $ch, CURLOPT_USERAGENT, "top-sdk-php" ); |
|
| 68 | + //https 请求 |
|
| 69 | + if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) { |
|
| 70 | + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
|
| 71 | + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + if (is_array($postFields) && 0 < count($postFields)) |
|
| 75 | + { |
|
| 76 | + $postBodyString = ""; |
|
| 77 | + $postMultipart = false; |
|
| 78 | + foreach ($postFields as $k => $v) |
|
| 79 | + { |
|
| 80 | + if("@" != substr($v, 0, 1))//判断是不是文件上传 |
|
| 81 | + { |
|
| 82 | + $postBodyString .= "$k=" . urlencode($v) . "&"; |
|
| 83 | + } |
|
| 84 | + else//文件上传用multipart/form-data,否则用www-form-urlencoded |
|
| 85 | + { |
|
| 86 | + $postMultipart = true; |
|
| 87 | + if(class_exists('\CURLFile')){ |
|
| 88 | + $postFields[$k] = new \CURLFile(substr($v, 1)); |
|
| 89 | + } |
|
| 90 | + } |
|
| 91 | + } |
|
| 92 | + unset($k, $v); |
|
| 93 | + curl_setopt($ch, CURLOPT_POST, true); |
|
| 94 | + if ($postMultipart) |
|
| 95 | + { |
|
| 96 | + if (class_exists('\CURLFile')) { |
|
| 97 | + curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true); |
|
| 98 | + } else { |
|
| 99 | + if (defined('CURLOPT_SAFE_UPLOAD')) { |
|
| 100 | + curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false); |
|
| 101 | + } |
|
| 102 | + } |
|
| 103 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); |
|
| 104 | + } |
|
| 105 | + else |
|
| 106 | + { |
|
| 107 | + $header = array("content-type: application/x-www-form-urlencoded; charset=UTF-8"); |
|
| 108 | + curl_setopt($ch,CURLOPT_HTTPHEADER,$header); |
|
| 109 | + curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1)); |
|
| 110 | + } |
|
| 111 | + } |
|
| 112 | + $reponse = curl_exec($ch); |
|
| 113 | 113 | |
| 114 | - if (curl_errno($ch)) |
|
| 115 | - { |
|
| 116 | - throw new Exception(curl_error($ch),0); |
|
| 117 | - } |
|
| 118 | - else |
|
| 119 | - { |
|
| 120 | - $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
|
| 121 | - if (200 !== $httpStatusCode) |
|
| 122 | - { |
|
| 123 | - throw new Exception($reponse,$httpStatusCode); |
|
| 124 | - } |
|
| 125 | - } |
|
| 126 | - curl_close($ch); |
|
| 127 | - return $reponse; |
|
| 128 | - } |
|
| 129 | - public function curl_with_memory_file($url, $postFields = null, $fileFields = null) |
|
| 130 | - { |
|
| 131 | - $ch = curl_init(); |
|
| 132 | - curl_setopt($ch, CURLOPT_URL, $url); |
|
| 133 | - curl_setopt($ch, CURLOPT_FAILONERROR, false); |
|
| 134 | - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
| 135 | - if ($this->readTimeout) { |
|
| 136 | - curl_setopt($ch, CURLOPT_TIMEOUT, $this->readTimeout); |
|
| 137 | - } |
|
| 138 | - if ($this->connectTimeout) { |
|
| 139 | - curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout); |
|
| 140 | - } |
|
| 141 | - curl_setopt ( $ch, CURLOPT_USERAGENT, "top-sdk-php" ); |
|
| 142 | - //https 请求 |
|
| 143 | - if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) { |
|
| 144 | - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
|
| 145 | - curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); |
|
| 146 | - } |
|
| 147 | - //生成分隔符 |
|
| 148 | - $delimiter = '-------------' . uniqid(); |
|
| 149 | - //先将post的普通数据生成主体字符串 |
|
| 150 | - $data = ''; |
|
| 151 | - if($postFields != null){ |
|
| 152 | - foreach ($postFields as $name => $content) { |
|
| 153 | - $data .= "--" . $delimiter . "\r\n"; |
|
| 154 | - $data .= 'Content-Disposition: form-data; name="' . $name . '"'; |
|
| 155 | - //multipart/form-data 不需要urlencode,参见 http:stackoverflow.com/questions/6603928/should-i-url-encode-post-data |
|
| 156 | - $data .= "\r\n\r\n" . $content . "\r\n"; |
|
| 157 | - } |
|
| 158 | - unset($name,$content); |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - //将上传的文件生成主体字符串 |
|
| 162 | - if($fileFields != null){ |
|
| 163 | - foreach ($fileFields as $name => $file) { |
|
| 164 | - $data .= "--" . $delimiter . "\r\n"; |
|
| 165 | - $data .= 'Content-Disposition: form-data; name="' . $name . '"; filename="' . $file['name'] . "\" \r\n"; |
|
| 166 | - $data .= 'Content-Type: ' . $file['type'] . "\r\n\r\n";//多了个文档类型 |
|
| 167 | - |
|
| 168 | - $data .= $file['content'] . "\r\n"; |
|
| 169 | - } |
|
| 170 | - unset($name,$file); |
|
| 171 | - } |
|
| 172 | - //主体结束的分隔符 |
|
| 173 | - $data .= "--" . $delimiter . "--"; |
|
| 174 | - |
|
| 175 | - curl_setopt($ch, CURLOPT_POST, true); |
|
| 176 | - curl_setopt($ch, CURLOPT_HTTPHEADER , array( |
|
| 177 | - 'Content-Type: multipart/form-data; boundary=' . $delimiter, |
|
| 178 | - 'Content-Length: ' . strlen($data)) |
|
| 179 | - ); |
|
| 180 | - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
|
| 181 | - curl_setopt($ch, CURLOPT_POSTFIELDS, $data); |
|
| 182 | - |
|
| 183 | - $reponse = curl_exec($ch); |
|
| 184 | - unset($data); |
|
| 185 | - |
|
| 186 | - if (curl_errno($ch)) |
|
| 187 | - { |
|
| 188 | - throw new Exception(curl_error($ch),0); |
|
| 189 | - } |
|
| 190 | - else |
|
| 191 | - { |
|
| 192 | - $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
|
| 193 | - if (200 !== $httpStatusCode) |
|
| 194 | - { |
|
| 195 | - throw new Exception($reponse,$httpStatusCode); |
|
| 196 | - } |
|
| 197 | - } |
|
| 198 | - curl_close($ch); |
|
| 199 | - return $reponse; |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - protected function logCommunicationError($apiName, $requestUrl, $errorCode, $responseTxt) |
|
| 203 | - { |
|
| 204 | - $localIp = isset($_SERVER["SERVER_ADDR"]) ? $_SERVER["SERVER_ADDR"] : "CLI"; |
|
| 205 | - $logger = new TopLogger; |
|
| 206 | - $logger->conf["log_file"] = rtrim(TOP_SDK_WORK_DIR, '\\/') . '/' . "logs/top_comm_err_" . $this->appkey . "_" . date("Y-m-d") . ".log"; |
|
| 207 | - $logger->conf["separator"] = "^_^"; |
|
| 208 | - $logData = array( |
|
| 209 | - date("Y-m-d H:i:s"), |
|
| 210 | - $apiName, |
|
| 211 | - $this->appkey, |
|
| 212 | - $localIp, |
|
| 213 | - PHP_OS, |
|
| 214 | - $this->sdkVersion, |
|
| 215 | - $requestUrl, |
|
| 216 | - $errorCode, |
|
| 217 | - str_replace("\n","",$responseTxt) |
|
| 218 | - ); |
|
| 219 | - $logger->log($logData); |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - public function execute($request, $session = null,$bestUrl = null) |
|
| 223 | - { |
|
| 224 | - if($this->gatewayUrl == null) { |
|
| 225 | - throw new Exception("client-check-error:Need Set gatewayUrl.", 40); |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - $result = new ResultSet(); |
|
| 229 | - if($this->checkRequest) { |
|
| 230 | - try { |
|
| 231 | - $request->check(); |
|
| 232 | - } catch (Exception $e) { |
|
| 233 | - |
|
| 234 | - $result->code = $e->getCode(); |
|
| 235 | - $result->msg = $e->getMessage(); |
|
| 236 | - return $result; |
|
| 237 | - } |
|
| 238 | - } |
|
| 239 | - //组装系统参数 |
|
| 240 | - $sysParams["app_key"] = $this->appkey; |
|
| 241 | - $sysParams["v"] = $this->apiVersion; |
|
| 242 | - $sysParams["format"] = $this->format; |
|
| 243 | - $sysParams["sign_method"] = $this->signMethod; |
|
| 244 | - $sysParams["method"] = $request->getApiMethodName(); |
|
| 245 | - $sysParams["timestamp"] = date("Y-m-d H:i:s"); |
|
| 246 | - $sysParams["target_app_key"] = $this->targetAppkey; |
|
| 247 | - if (null != $session) |
|
| 248 | - { |
|
| 249 | - $sysParams["session"] = $session; |
|
| 250 | - } |
|
| 251 | - $apiParams = array(); |
|
| 252 | - //获取业务参数 |
|
| 253 | - $apiParams = $request->getApiParas(); |
|
| 254 | - |
|
| 255 | - |
|
| 256 | - //系统参数放入GET请求串 |
|
| 257 | - if($bestUrl){ |
|
| 258 | - $requestUrl = $bestUrl."?"; |
|
| 259 | - $sysParams["partner_id"] = $this->getClusterTag(); |
|
| 260 | - }else{ |
|
| 261 | - $requestUrl = $this->gatewayUrl."?"; |
|
| 262 | - $sysParams["partner_id"] = $this->sdkVersion; |
|
| 263 | - } |
|
| 264 | - //签名 |
|
| 265 | - $sysParams["sign"] = $this->generateSign(array_merge($apiParams, $sysParams)); |
|
| 266 | - |
|
| 267 | - foreach ($sysParams as $sysParamKey => $sysParamValue) |
|
| 268 | - { |
|
| 269 | - // if(strcmp($sysParamKey,"timestamp") != 0) |
|
| 270 | - $requestUrl .= "$sysParamKey=" . urlencode($sysParamValue) . "&"; |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - $fileFields = array(); |
|
| 274 | - foreach ($apiParams as $key => $value) { |
|
| 275 | - if(is_array($value) && array_key_exists('type',$value) && array_key_exists('content',$value) ){ |
|
| 276 | - $value['name'] = $key; |
|
| 277 | - $fileFields[$key] = $value; |
|
| 278 | - unset($apiParams[$key]); |
|
| 279 | - } |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - // $requestUrl .= "timestamp=" . urlencode($sysParams["timestamp"]) . "&"; |
|
| 283 | - $requestUrl = substr($requestUrl, 0, -1); |
|
| 284 | - |
|
| 285 | - //发起HTTP请求 |
|
| 286 | - try |
|
| 287 | - { |
|
| 288 | - if(count($fileFields) > 0){ |
|
| 289 | - $resp = $this->curl_with_memory_file($requestUrl, $apiParams, $fileFields); |
|
| 290 | - }else{ |
|
| 291 | - $resp = $this->curl($requestUrl, $apiParams); |
|
| 292 | - } |
|
| 293 | - } |
|
| 294 | - catch (Exception $e) |
|
| 295 | - { |
|
| 296 | - $this->logCommunicationError($sysParams["method"],$requestUrl,"HTTP_ERROR_" . $e->getCode(),$e->getMessage()); |
|
| 297 | - $result->code = $e->getCode(); |
|
| 298 | - $result->msg = $e->getMessage(); |
|
| 299 | - return $result; |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - unset($apiParams); |
|
| 303 | - unset($fileFields); |
|
| 304 | - //解析TOP返回结果 |
|
| 305 | - $respWellFormed = false; |
|
| 306 | - if ("json" == $this->format) |
|
| 307 | - { |
|
| 308 | - $respObject = json_decode($resp); |
|
| 309 | - if (null !== $respObject) |
|
| 310 | - { |
|
| 311 | - $respWellFormed = true; |
|
| 312 | - foreach ($respObject as $propKey => $propValue) |
|
| 313 | - { |
|
| 314 | - $respObject = $propValue; |
|
| 315 | - } |
|
| 316 | - } |
|
| 317 | - } |
|
| 318 | - else if("xml" == $this->format) |
|
| 319 | - { |
|
| 320 | - $respObject = @simplexml_load_string($resp); |
|
| 321 | - if (false !== $respObject) |
|
| 322 | - { |
|
| 323 | - $respWellFormed = true; |
|
| 324 | - } |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - //返回的HTTP文本不是标准JSON或者XML,记下错误日志 |
|
| 328 | - if (false === $respWellFormed) |
|
| 329 | - { |
|
| 330 | - $this->logCommunicationError($sysParams["method"],$requestUrl,"HTTP_RESPONSE_NOT_WELL_FORMED",$resp); |
|
| 331 | - $result->code = 0; |
|
| 332 | - $result->msg = "HTTP_RESPONSE_NOT_WELL_FORMED"; |
|
| 333 | - return $result; |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - //如果TOP返回了错误码,记录到业务错误日志中 |
|
| 337 | - if (isset($respObject->code)) |
|
| 338 | - { |
|
| 339 | - $logger = new TopLogger; |
|
| 340 | - $logger->conf["log_file"] = rtrim(TOP_SDK_WORK_DIR, '\\/') . '/' . "logs/top_biz_err_" . $this->appkey . "_" . date("Y-m-d") . ".log"; |
|
| 341 | - $logger->log(array( |
|
| 342 | - date("Y-m-d H:i:s"), |
|
| 343 | - $resp |
|
| 344 | - )); |
|
| 345 | - } |
|
| 346 | - return $respObject; |
|
| 347 | - } |
|
| 348 | - |
|
| 349 | - public function exec($paramsArray) |
|
| 350 | - { |
|
| 351 | - if (!isset($paramsArray["method"])) |
|
| 352 | - { |
|
| 353 | - trigger_error("No api name passed"); |
|
| 354 | - } |
|
| 355 | - $inflector = new LtInflector; |
|
| 356 | - $inflector->conf["separator"] = "."; |
|
| 357 | - $requestClassName = ucfirst($inflector->camelize(substr($paramsArray["method"], 7))) . "Request"; |
|
| 358 | - if (!class_exists($requestClassName)) |
|
| 359 | - { |
|
| 360 | - trigger_error("No such api: " . $paramsArray["method"]); |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - $session = isset($paramsArray["session"]) ? $paramsArray["session"] : null; |
|
| 364 | - |
|
| 365 | - $req = new $requestClassName; |
|
| 366 | - foreach($paramsArray as $paraKey => $paraValue) |
|
| 367 | - { |
|
| 368 | - $inflector->conf["separator"] = "_"; |
|
| 369 | - $setterMethodName = $inflector->camelize($paraKey); |
|
| 370 | - $inflector->conf["separator"] = "."; |
|
| 371 | - $setterMethodName = "set" . $inflector->camelize($setterMethodName); |
|
| 372 | - if (method_exists($req, $setterMethodName)) |
|
| 373 | - { |
|
| 374 | - $req->$setterMethodName($paraValue); |
|
| 375 | - } |
|
| 376 | - } |
|
| 377 | - return $this->execute($req, $session); |
|
| 378 | - } |
|
| 379 | - |
|
| 380 | - private function getClusterTag() |
|
| 114 | + if (curl_errno($ch)) |
|
| 115 | + { |
|
| 116 | + throw new Exception(curl_error($ch),0); |
|
| 117 | + } |
|
| 118 | + else |
|
| 119 | + { |
|
| 120 | + $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
|
| 121 | + if (200 !== $httpStatusCode) |
|
| 122 | + { |
|
| 123 | + throw new Exception($reponse,$httpStatusCode); |
|
| 124 | + } |
|
| 125 | + } |
|
| 126 | + curl_close($ch); |
|
| 127 | + return $reponse; |
|
| 128 | + } |
|
| 129 | + public function curl_with_memory_file($url, $postFields = null, $fileFields = null) |
|
| 130 | + { |
|
| 131 | + $ch = curl_init(); |
|
| 132 | + curl_setopt($ch, CURLOPT_URL, $url); |
|
| 133 | + curl_setopt($ch, CURLOPT_FAILONERROR, false); |
|
| 134 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
| 135 | + if ($this->readTimeout) { |
|
| 136 | + curl_setopt($ch, CURLOPT_TIMEOUT, $this->readTimeout); |
|
| 137 | + } |
|
| 138 | + if ($this->connectTimeout) { |
|
| 139 | + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout); |
|
| 140 | + } |
|
| 141 | + curl_setopt ( $ch, CURLOPT_USERAGENT, "top-sdk-php" ); |
|
| 142 | + //https 请求 |
|
| 143 | + if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) { |
|
| 144 | + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
|
| 145 | + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); |
|
| 146 | + } |
|
| 147 | + //生成分隔符 |
|
| 148 | + $delimiter = '-------------' . uniqid(); |
|
| 149 | + //先将post的普通数据生成主体字符串 |
|
| 150 | + $data = ''; |
|
| 151 | + if($postFields != null){ |
|
| 152 | + foreach ($postFields as $name => $content) { |
|
| 153 | + $data .= "--" . $delimiter . "\r\n"; |
|
| 154 | + $data .= 'Content-Disposition: form-data; name="' . $name . '"'; |
|
| 155 | + //multipart/form-data 不需要urlencode,参见 http:stackoverflow.com/questions/6603928/should-i-url-encode-post-data |
|
| 156 | + $data .= "\r\n\r\n" . $content . "\r\n"; |
|
| 157 | + } |
|
| 158 | + unset($name,$content); |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + //将上传的文件生成主体字符串 |
|
| 162 | + if($fileFields != null){ |
|
| 163 | + foreach ($fileFields as $name => $file) { |
|
| 164 | + $data .= "--" . $delimiter . "\r\n"; |
|
| 165 | + $data .= 'Content-Disposition: form-data; name="' . $name . '"; filename="' . $file['name'] . "\" \r\n"; |
|
| 166 | + $data .= 'Content-Type: ' . $file['type'] . "\r\n\r\n";//多了个文档类型 |
|
| 167 | + |
|
| 168 | + $data .= $file['content'] . "\r\n"; |
|
| 169 | + } |
|
| 170 | + unset($name,$file); |
|
| 171 | + } |
|
| 172 | + //主体结束的分隔符 |
|
| 173 | + $data .= "--" . $delimiter . "--"; |
|
| 174 | + |
|
| 175 | + curl_setopt($ch, CURLOPT_POST, true); |
|
| 176 | + curl_setopt($ch, CURLOPT_HTTPHEADER , array( |
|
| 177 | + 'Content-Type: multipart/form-data; boundary=' . $delimiter, |
|
| 178 | + 'Content-Length: ' . strlen($data)) |
|
| 179 | + ); |
|
| 180 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
|
| 181 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $data); |
|
| 182 | + |
|
| 183 | + $reponse = curl_exec($ch); |
|
| 184 | + unset($data); |
|
| 185 | + |
|
| 186 | + if (curl_errno($ch)) |
|
| 187 | + { |
|
| 188 | + throw new Exception(curl_error($ch),0); |
|
| 189 | + } |
|
| 190 | + else |
|
| 191 | + { |
|
| 192 | + $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
|
| 193 | + if (200 !== $httpStatusCode) |
|
| 194 | + { |
|
| 195 | + throw new Exception($reponse,$httpStatusCode); |
|
| 196 | + } |
|
| 197 | + } |
|
| 198 | + curl_close($ch); |
|
| 199 | + return $reponse; |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + protected function logCommunicationError($apiName, $requestUrl, $errorCode, $responseTxt) |
|
| 203 | + { |
|
| 204 | + $localIp = isset($_SERVER["SERVER_ADDR"]) ? $_SERVER["SERVER_ADDR"] : "CLI"; |
|
| 205 | + $logger = new TopLogger; |
|
| 206 | + $logger->conf["log_file"] = rtrim(TOP_SDK_WORK_DIR, '\\/') . '/' . "logs/top_comm_err_" . $this->appkey . "_" . date("Y-m-d") . ".log"; |
|
| 207 | + $logger->conf["separator"] = "^_^"; |
|
| 208 | + $logData = array( |
|
| 209 | + date("Y-m-d H:i:s"), |
|
| 210 | + $apiName, |
|
| 211 | + $this->appkey, |
|
| 212 | + $localIp, |
|
| 213 | + PHP_OS, |
|
| 214 | + $this->sdkVersion, |
|
| 215 | + $requestUrl, |
|
| 216 | + $errorCode, |
|
| 217 | + str_replace("\n","",$responseTxt) |
|
| 218 | + ); |
|
| 219 | + $logger->log($logData); |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + public function execute($request, $session = null,$bestUrl = null) |
|
| 223 | + { |
|
| 224 | + if($this->gatewayUrl == null) { |
|
| 225 | + throw new Exception("client-check-error:Need Set gatewayUrl.", 40); |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + $result = new ResultSet(); |
|
| 229 | + if($this->checkRequest) { |
|
| 230 | + try { |
|
| 231 | + $request->check(); |
|
| 232 | + } catch (Exception $e) { |
|
| 233 | + |
|
| 234 | + $result->code = $e->getCode(); |
|
| 235 | + $result->msg = $e->getMessage(); |
|
| 236 | + return $result; |
|
| 237 | + } |
|
| 238 | + } |
|
| 239 | + //组装系统参数 |
|
| 240 | + $sysParams["app_key"] = $this->appkey; |
|
| 241 | + $sysParams["v"] = $this->apiVersion; |
|
| 242 | + $sysParams["format"] = $this->format; |
|
| 243 | + $sysParams["sign_method"] = $this->signMethod; |
|
| 244 | + $sysParams["method"] = $request->getApiMethodName(); |
|
| 245 | + $sysParams["timestamp"] = date("Y-m-d H:i:s"); |
|
| 246 | + $sysParams["target_app_key"] = $this->targetAppkey; |
|
| 247 | + if (null != $session) |
|
| 248 | + { |
|
| 249 | + $sysParams["session"] = $session; |
|
| 250 | + } |
|
| 251 | + $apiParams = array(); |
|
| 252 | + //获取业务参数 |
|
| 253 | + $apiParams = $request->getApiParas(); |
|
| 254 | + |
|
| 255 | + |
|
| 256 | + //系统参数放入GET请求串 |
|
| 257 | + if($bestUrl){ |
|
| 258 | + $requestUrl = $bestUrl."?"; |
|
| 259 | + $sysParams["partner_id"] = $this->getClusterTag(); |
|
| 260 | + }else{ |
|
| 261 | + $requestUrl = $this->gatewayUrl."?"; |
|
| 262 | + $sysParams["partner_id"] = $this->sdkVersion; |
|
| 263 | + } |
|
| 264 | + //签名 |
|
| 265 | + $sysParams["sign"] = $this->generateSign(array_merge($apiParams, $sysParams)); |
|
| 266 | + |
|
| 267 | + foreach ($sysParams as $sysParamKey => $sysParamValue) |
|
| 268 | + { |
|
| 269 | + // if(strcmp($sysParamKey,"timestamp") != 0) |
|
| 270 | + $requestUrl .= "$sysParamKey=" . urlencode($sysParamValue) . "&"; |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + $fileFields = array(); |
|
| 274 | + foreach ($apiParams as $key => $value) { |
|
| 275 | + if(is_array($value) && array_key_exists('type',$value) && array_key_exists('content',$value) ){ |
|
| 276 | + $value['name'] = $key; |
|
| 277 | + $fileFields[$key] = $value; |
|
| 278 | + unset($apiParams[$key]); |
|
| 279 | + } |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + // $requestUrl .= "timestamp=" . urlencode($sysParams["timestamp"]) . "&"; |
|
| 283 | + $requestUrl = substr($requestUrl, 0, -1); |
|
| 284 | + |
|
| 285 | + //发起HTTP请求 |
|
| 286 | + try |
|
| 287 | + { |
|
| 288 | + if(count($fileFields) > 0){ |
|
| 289 | + $resp = $this->curl_with_memory_file($requestUrl, $apiParams, $fileFields); |
|
| 290 | + }else{ |
|
| 291 | + $resp = $this->curl($requestUrl, $apiParams); |
|
| 292 | + } |
|
| 293 | + } |
|
| 294 | + catch (Exception $e) |
|
| 295 | + { |
|
| 296 | + $this->logCommunicationError($sysParams["method"],$requestUrl,"HTTP_ERROR_" . $e->getCode(),$e->getMessage()); |
|
| 297 | + $result->code = $e->getCode(); |
|
| 298 | + $result->msg = $e->getMessage(); |
|
| 299 | + return $result; |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + unset($apiParams); |
|
| 303 | + unset($fileFields); |
|
| 304 | + //解析TOP返回结果 |
|
| 305 | + $respWellFormed = false; |
|
| 306 | + if ("json" == $this->format) |
|
| 307 | + { |
|
| 308 | + $respObject = json_decode($resp); |
|
| 309 | + if (null !== $respObject) |
|
| 310 | + { |
|
| 311 | + $respWellFormed = true; |
|
| 312 | + foreach ($respObject as $propKey => $propValue) |
|
| 313 | + { |
|
| 314 | + $respObject = $propValue; |
|
| 315 | + } |
|
| 316 | + } |
|
| 317 | + } |
|
| 318 | + else if("xml" == $this->format) |
|
| 319 | + { |
|
| 320 | + $respObject = @simplexml_load_string($resp); |
|
| 321 | + if (false !== $respObject) |
|
| 322 | + { |
|
| 323 | + $respWellFormed = true; |
|
| 324 | + } |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + //返回的HTTP文本不是标准JSON或者XML,记下错误日志 |
|
| 328 | + if (false === $respWellFormed) |
|
| 329 | + { |
|
| 330 | + $this->logCommunicationError($sysParams["method"],$requestUrl,"HTTP_RESPONSE_NOT_WELL_FORMED",$resp); |
|
| 331 | + $result->code = 0; |
|
| 332 | + $result->msg = "HTTP_RESPONSE_NOT_WELL_FORMED"; |
|
| 333 | + return $result; |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + //如果TOP返回了错误码,记录到业务错误日志中 |
|
| 337 | + if (isset($respObject->code)) |
|
| 338 | + { |
|
| 339 | + $logger = new TopLogger; |
|
| 340 | + $logger->conf["log_file"] = rtrim(TOP_SDK_WORK_DIR, '\\/') . '/' . "logs/top_biz_err_" . $this->appkey . "_" . date("Y-m-d") . ".log"; |
|
| 341 | + $logger->log(array( |
|
| 342 | + date("Y-m-d H:i:s"), |
|
| 343 | + $resp |
|
| 344 | + )); |
|
| 345 | + } |
|
| 346 | + return $respObject; |
|
| 347 | + } |
|
| 348 | + |
|
| 349 | + public function exec($paramsArray) |
|
| 350 | + { |
|
| 351 | + if (!isset($paramsArray["method"])) |
|
| 352 | + { |
|
| 353 | + trigger_error("No api name passed"); |
|
| 354 | + } |
|
| 355 | + $inflector = new LtInflector; |
|
| 356 | + $inflector->conf["separator"] = "."; |
|
| 357 | + $requestClassName = ucfirst($inflector->camelize(substr($paramsArray["method"], 7))) . "Request"; |
|
| 358 | + if (!class_exists($requestClassName)) |
|
| 359 | + { |
|
| 360 | + trigger_error("No such api: " . $paramsArray["method"]); |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + $session = isset($paramsArray["session"]) ? $paramsArray["session"] : null; |
|
| 364 | + |
|
| 365 | + $req = new $requestClassName; |
|
| 366 | + foreach($paramsArray as $paraKey => $paraValue) |
|
| 367 | + { |
|
| 368 | + $inflector->conf["separator"] = "_"; |
|
| 369 | + $setterMethodName = $inflector->camelize($paraKey); |
|
| 370 | + $inflector->conf["separator"] = "."; |
|
| 371 | + $setterMethodName = "set" . $inflector->camelize($setterMethodName); |
|
| 372 | + if (method_exists($req, $setterMethodName)) |
|
| 373 | + { |
|
| 374 | + $req->$setterMethodName($paraValue); |
|
| 375 | + } |
|
| 376 | + } |
|
| 377 | + return $this->execute($req, $session); |
|
| 378 | + } |
|
| 379 | + |
|
| 380 | + private function getClusterTag() |
|
| 381 | 381 | { |
| 382 | - return substr($this->sdkVersion,0,11)."-cluster".substr($this->sdkVersion,11); |
|
| 382 | + return substr($this->sdkVersion,0,11)."-cluster".substr($this->sdkVersion,11); |
|
| 383 | 383 | } |
| 384 | 384 | } |
@@ -2,22 +2,22 @@ |
||
| 2 | 2 | |
| 3 | 3 | class HttpdnsGetRequest |
| 4 | 4 | { |
| 5 | - private $apiParas = array(); |
|
| 5 | + private $apiParas = array(); |
|
| 6 | 6 | |
| 7 | - public function getApiMethodName() |
|
| 8 | - { |
|
| 9 | - return "taobao.httpdns.get"; |
|
| 10 | - } |
|
| 7 | + public function getApiMethodName() |
|
| 8 | + { |
|
| 9 | + return "taobao.httpdns.get"; |
|
| 10 | + } |
|
| 11 | 11 | |
| 12 | - public function getApiParas() |
|
| 13 | - { |
|
| 14 | - return $this->apiParas; |
|
| 15 | - } |
|
| 12 | + public function getApiParas() |
|
| 13 | + { |
|
| 14 | + return $this->apiParas; |
|
| 15 | + } |
|
| 16 | 16 | |
| 17 | - public function check(){} |
|
| 17 | + public function check(){} |
|
| 18 | 18 | |
| 19 | - public function putOtherTextParam($key, $value) { |
|
| 20 | - $this->apiParas[$key] = $value; |
|
| 21 | - $this->$key = $value; |
|
| 22 | - } |
|
| 19 | + public function putOtherTextParam($key, $value) { |
|
| 20 | + $this->apiParas[$key] = $value; |
|
| 21 | + $this->$key = $value; |
|
| 22 | + } |
|
| 23 | 23 | } |
@@ -1,219 +1,219 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | class SpiUtils{ |
| 3 | - private static $top_sign_list = "HTTP_TOP_SIGN_LIST"; |
|
| 4 | - private static $timestamp = "timestamp"; |
|
| 5 | - private static $header_real_ip = array("X_Real_IP", "X_Forwarded_For", "Proxy_Client_IP", |
|
| 6 | - "WL_Proxy_Client_IP", "HTTP_CLIENT_IP", "HTTP_X_FORWARDED_FOR"); |
|
| 7 | - /** |
|
| 8 | - * 校验SPI请求签名,适用于所有GET请求,及不包含文件参数的POST请求。 |
|
| 9 | - * |
|
| 10 | - * @param request 请求对象 |
|
| 11 | - * @param secret app对应的secret |
|
| 12 | - * @return true:校验通过;false:校验不通过 |
|
| 13 | - */ |
|
| 14 | - public static function checkSign4FormRequest($secret){ |
|
| 15 | - return self::checkSign(null,null,$secret); |
|
| 16 | - } |
|
| 17 | - |
|
| 18 | - /** |
|
| 19 | - * 校验SPI请求签名,适用于请求体是xml/json等可用文本表示的POST请求。 |
|
| 20 | - * |
|
| 21 | - * @param request 请求对象 |
|
| 22 | - * @param body 请求体的文本内容 |
|
| 23 | - * @param secret app对应的secret |
|
| 24 | - * @return true:校验通过;false:校验不通过 |
|
| 25 | - */ |
|
| 26 | - public static function checkSign4TextRequest($body,$secret){ |
|
| 27 | - return self::checkSign(null,$body,$secret); |
|
| 28 | - } |
|
| 3 | + private static $top_sign_list = "HTTP_TOP_SIGN_LIST"; |
|
| 4 | + private static $timestamp = "timestamp"; |
|
| 5 | + private static $header_real_ip = array("X_Real_IP", "X_Forwarded_For", "Proxy_Client_IP", |
|
| 6 | + "WL_Proxy_Client_IP", "HTTP_CLIENT_IP", "HTTP_X_FORWARDED_FOR"); |
|
| 7 | + /** |
|
| 8 | + * 校验SPI请求签名,适用于所有GET请求,及不包含文件参数的POST请求。 |
|
| 9 | + * |
|
| 10 | + * @param request 请求对象 |
|
| 11 | + * @param secret app对应的secret |
|
| 12 | + * @return true:校验通过;false:校验不通过 |
|
| 13 | + */ |
|
| 14 | + public static function checkSign4FormRequest($secret){ |
|
| 15 | + return self::checkSign(null,null,$secret); |
|
| 16 | + } |
|
| 17 | + |
|
| 18 | + /** |
|
| 19 | + * 校验SPI请求签名,适用于请求体是xml/json等可用文本表示的POST请求。 |
|
| 20 | + * |
|
| 21 | + * @param request 请求对象 |
|
| 22 | + * @param body 请求体的文本内容 |
|
| 23 | + * @param secret app对应的secret |
|
| 24 | + * @return true:校验通过;false:校验不通过 |
|
| 25 | + */ |
|
| 26 | + public static function checkSign4TextRequest($body,$secret){ |
|
| 27 | + return self::checkSign(null,$body,$secret); |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * 校验SPI请求签名,适用于带文件上传的POST请求。 |
|
| 32 | - * |
|
| 33 | - * @param request 请求对象 |
|
| 34 | - * @param form 除了文件参数以外的所有普通文本参数的map集合 |
|
| 35 | - * @param secret app对应的secret |
|
| 36 | - * @return true:校验通过;false:校验不通过 |
|
| 37 | - */ |
|
| 38 | - public static function checkSign4FileRequest($form, $secret){ |
|
| 39 | - return self::checkSign($form, null, $secret); |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - private static function checkSign($form, $body, $secret) { |
|
| 43 | - $params = array(); |
|
| 44 | - // 1. 获取header参数 |
|
| 45 | - $headerMap = self::getHeaderMap(); |
|
| 46 | - foreach ($headerMap as $k => $v){ |
|
| 47 | - $params[$k] = $v ; |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - // 2. 获取url参数 |
|
| 51 | - $queryMap = self::getQueryMap(); |
|
| 52 | - foreach ($queryMap as $k => $v){ |
|
| 53 | - $params[$k] = $v ; |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - // 3. 获取form参数 |
|
| 57 | - if ($form == null && $body == null) { |
|
| 58 | - $formMap = self::getFormMap(); |
|
| 59 | - foreach ($formMap as $k => $v){ |
|
| 60 | - $params[$k] = $v ; |
|
| 61 | - } |
|
| 62 | - } else if ($form != null) { |
|
| 63 | - foreach ($form as $k => $v){ |
|
| 64 | - $params[$k] = $v ; |
|
| 65 | - } |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - if($body == null){ |
|
| 69 | - $body = file_get_contents('php://input'); |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - $remoteSign = $queryMap["sign"]; |
|
| 73 | - $localSign = self::sign($params, $body, $secret); |
|
| 74 | - if (strcmp($remoteSign, $localSign) == 0) { |
|
| 75 | - return true; |
|
| 76 | - } else { |
|
| 77 | - $paramStr = self::getParamStrFromMap($params); |
|
| 78 | - self::logCommunicationError($remoteSign,$localSign,$paramStr,$body); |
|
| 79 | - return false; |
|
| 80 | - } |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - private static function getHeaderMap() { |
|
| 84 | - $headerMap = array(); |
|
| 85 | - $signList = $_SERVER['HTTP_TOP_SIGN_LIST']; // 只获取参与签名的头部字段 |
|
| 86 | - |
|
| 87 | - if(!$signList) { |
|
| 88 | - return $headerMap; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - $signList = trim($signList); |
|
| 92 | - if (strlen($signList) > 0){ |
|
| 93 | - $params = split(",", $signList); |
|
| 94 | - foreach ($_SERVER as $k => $v){ |
|
| 95 | - if (substr($k, 0, 5) == 'HTTP_'){ |
|
| 96 | - foreach($params as $kk){ |
|
| 97 | - $upperkey = strtoupper($kk); |
|
| 98 | - if(self::endWith($k,$upperkey)){ |
|
| 99 | - $headerMap[$kk] = $v; |
|
| 100 | - } |
|
| 101 | - } |
|
| 102 | - } |
|
| 103 | - } |
|
| 104 | - } |
|
| 105 | - return $headerMap; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - private static function getQueryMap(){ |
|
| 109 | - $queryStr = $_SERVER["QUERY_STRING"]; |
|
| 110 | - $resultArray = array(); |
|
| 111 | - foreach (explode('&', $queryStr) as $pair) { |
|
| 112 | - list($key, $value) = explode('=', $pair); |
|
| 113 | - if (strpos($key, '.') !== false) { |
|
| 114 | - list($subKey, $subVal) = explode('.', $key); |
|
| 115 | - |
|
| 116 | - if (preg_match('/(?P<name>\w+)\[(?P<index>\w+)\]/', $subKey, $matches)) { |
|
| 117 | - $resultArray[$matches['name']][$matches['index']][$subVal] = $value; |
|
| 118 | - } else { |
|
| 119 | - $resultArray[$subKey][$subVal] = urldecode($value); |
|
| 120 | - } |
|
| 121 | - } else { |
|
| 122 | - $resultArray[$key] = urldecode($value); |
|
| 123 | - } |
|
| 124 | - } |
|
| 125 | - return $resultArray; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - private static function checkRemoteIp(){ |
|
| 129 | - $remoteIp = $_SERVER["REMOTE_ADDR"]; |
|
| 130 | - foreach ($header_real_ip as $k){ |
|
| 131 | - $realIp = $_SERVER[$k]; |
|
| 132 | - $realIp = trim($realIp); |
|
| 133 | - if(strlen($realIp) > 0 && strcasecmp("unknown",$realIp)){ |
|
| 134 | - $remoteIp = $realIp; |
|
| 135 | - break; |
|
| 136 | - } |
|
| 137 | - } |
|
| 138 | - return self::startsWith($remoteIp,"140.205.144.") || self::startsWith($remoteIp,"40.205.145."); |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - private static function getFormMap(){ |
|
| 142 | - $resultArray = array(); |
|
| 143 | - foreach($_POST as $key=>$v) { |
|
| 144 | - $resultArray[$key] = $v ; |
|
| 145 | - } |
|
| 146 | - return $resultArray ; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - private static function startsWith($haystack, $needle) { |
|
| 150 | - return $needle === "" || strpos($haystack, $needle) === 0; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - private static function endWith($haystack, $needle) { |
|
| 154 | - $length = strlen($needle); |
|
| 155 | - if($length == 0) |
|
| 156 | - { |
|
| 157 | - return true; |
|
| 158 | - } |
|
| 159 | - return (substr($haystack, -$length) === $needle); |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - private static function checkTimestamp(){ |
|
| 163 | - $ts = $_POST['timestamp']; |
|
| 164 | - if($ts){ |
|
| 165 | - $clientTimestamp = strtotime($ts); |
|
| 166 | - $current = $_SERVER['REQUEST_TIME']; |
|
| 167 | - return ($current - $clientTimestamp) <= 5*60*1000; |
|
| 168 | - }else{ |
|
| 169 | - return false; |
|
| 170 | - } |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - private static function getParamStrFromMap($params){ |
|
| 174 | - ksort($params); |
|
| 175 | - $stringToBeSigned = ""; |
|
| 176 | - foreach ($params as $k => $v) |
|
| 177 | - { |
|
| 178 | - if(strcmp("sign", $k) != 0) |
|
| 179 | - { |
|
| 180 | - $stringToBeSigned .= "$k$v"; |
|
| 181 | - } |
|
| 182 | - } |
|
| 183 | - unset($k, $v); |
|
| 184 | - return $stringToBeSigned; |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - private static function sign($params,$body,$secret){ |
|
| 188 | - ksort($params); |
|
| 189 | - |
|
| 190 | - $stringToBeSigned = $secret; |
|
| 191 | - $stringToBeSigned .= self::getParamStrFromMap($params); |
|
| 192 | - |
|
| 193 | - if($body) |
|
| 194 | - $stringToBeSigned .= $body; |
|
| 195 | - $stringToBeSigned .= $secret; |
|
| 196 | - return strtoupper(md5($stringToBeSigned)); |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - protected static function logCommunicationError($remoteSign, $localSign, $paramStr, $body) |
|
| 200 | - { |
|
| 201 | - $localIp = isset($_SERVER["SERVER_ADDR"]) ? $_SERVER["SERVER_ADDR"] : "CLI"; |
|
| 202 | - $logger = new TopLogger; |
|
| 203 | - $logger->conf["log_file"] = rtrim(TOP_SDK_WORK_DIR, '\\/') . '/' . "logs/top_comm_err_". date("Y-m-d") . ".log"; |
|
| 204 | - $logger->conf["separator"] = "^_^"; |
|
| 205 | - $logData = array( |
|
| 206 | - "checkTopSign error" , |
|
| 207 | - "remoteSign=".$remoteSign , |
|
| 208 | - "localSign=".$localSign , |
|
| 209 | - "paramStr=".$paramStr , |
|
| 210 | - "body=".$body |
|
| 211 | - ); |
|
| 212 | - $logger->log($logData); |
|
| 213 | - } |
|
| 214 | - private static function clear_blank($str, $glue='') |
|
| 215 | - { |
|
| 216 | - $replace = array(" ", "\r", "\n", "\t"); return str_replace($replace, $glue, $str); |
|
| 217 | - } |
|
| 30 | + /** |
|
| 31 | + * 校验SPI请求签名,适用于带文件上传的POST请求。 |
|
| 32 | + * |
|
| 33 | + * @param request 请求对象 |
|
| 34 | + * @param form 除了文件参数以外的所有普通文本参数的map集合 |
|
| 35 | + * @param secret app对应的secret |
|
| 36 | + * @return true:校验通过;false:校验不通过 |
|
| 37 | + */ |
|
| 38 | + public static function checkSign4FileRequest($form, $secret){ |
|
| 39 | + return self::checkSign($form, null, $secret); |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + private static function checkSign($form, $body, $secret) { |
|
| 43 | + $params = array(); |
|
| 44 | + // 1. 获取header参数 |
|
| 45 | + $headerMap = self::getHeaderMap(); |
|
| 46 | + foreach ($headerMap as $k => $v){ |
|
| 47 | + $params[$k] = $v ; |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + // 2. 获取url参数 |
|
| 51 | + $queryMap = self::getQueryMap(); |
|
| 52 | + foreach ($queryMap as $k => $v){ |
|
| 53 | + $params[$k] = $v ; |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + // 3. 获取form参数 |
|
| 57 | + if ($form == null && $body == null) { |
|
| 58 | + $formMap = self::getFormMap(); |
|
| 59 | + foreach ($formMap as $k => $v){ |
|
| 60 | + $params[$k] = $v ; |
|
| 61 | + } |
|
| 62 | + } else if ($form != null) { |
|
| 63 | + foreach ($form as $k => $v){ |
|
| 64 | + $params[$k] = $v ; |
|
| 65 | + } |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + if($body == null){ |
|
| 69 | + $body = file_get_contents('php://input'); |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + $remoteSign = $queryMap["sign"]; |
|
| 73 | + $localSign = self::sign($params, $body, $secret); |
|
| 74 | + if (strcmp($remoteSign, $localSign) == 0) { |
|
| 75 | + return true; |
|
| 76 | + } else { |
|
| 77 | + $paramStr = self::getParamStrFromMap($params); |
|
| 78 | + self::logCommunicationError($remoteSign,$localSign,$paramStr,$body); |
|
| 79 | + return false; |
|
| 80 | + } |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + private static function getHeaderMap() { |
|
| 84 | + $headerMap = array(); |
|
| 85 | + $signList = $_SERVER['HTTP_TOP_SIGN_LIST']; // 只获取参与签名的头部字段 |
|
| 86 | + |
|
| 87 | + if(!$signList) { |
|
| 88 | + return $headerMap; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + $signList = trim($signList); |
|
| 92 | + if (strlen($signList) > 0){ |
|
| 93 | + $params = split(",", $signList); |
|
| 94 | + foreach ($_SERVER as $k => $v){ |
|
| 95 | + if (substr($k, 0, 5) == 'HTTP_'){ |
|
| 96 | + foreach($params as $kk){ |
|
| 97 | + $upperkey = strtoupper($kk); |
|
| 98 | + if(self::endWith($k,$upperkey)){ |
|
| 99 | + $headerMap[$kk] = $v; |
|
| 100 | + } |
|
| 101 | + } |
|
| 102 | + } |
|
| 103 | + } |
|
| 104 | + } |
|
| 105 | + return $headerMap; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + private static function getQueryMap(){ |
|
| 109 | + $queryStr = $_SERVER["QUERY_STRING"]; |
|
| 110 | + $resultArray = array(); |
|
| 111 | + foreach (explode('&', $queryStr) as $pair) { |
|
| 112 | + list($key, $value) = explode('=', $pair); |
|
| 113 | + if (strpos($key, '.') !== false) { |
|
| 114 | + list($subKey, $subVal) = explode('.', $key); |
|
| 115 | + |
|
| 116 | + if (preg_match('/(?P<name>\w+)\[(?P<index>\w+)\]/', $subKey, $matches)) { |
|
| 117 | + $resultArray[$matches['name']][$matches['index']][$subVal] = $value; |
|
| 118 | + } else { |
|
| 119 | + $resultArray[$subKey][$subVal] = urldecode($value); |
|
| 120 | + } |
|
| 121 | + } else { |
|
| 122 | + $resultArray[$key] = urldecode($value); |
|
| 123 | + } |
|
| 124 | + } |
|
| 125 | + return $resultArray; |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + private static function checkRemoteIp(){ |
|
| 129 | + $remoteIp = $_SERVER["REMOTE_ADDR"]; |
|
| 130 | + foreach ($header_real_ip as $k){ |
|
| 131 | + $realIp = $_SERVER[$k]; |
|
| 132 | + $realIp = trim($realIp); |
|
| 133 | + if(strlen($realIp) > 0 && strcasecmp("unknown",$realIp)){ |
|
| 134 | + $remoteIp = $realIp; |
|
| 135 | + break; |
|
| 136 | + } |
|
| 137 | + } |
|
| 138 | + return self::startsWith($remoteIp,"140.205.144.") || self::startsWith($remoteIp,"40.205.145."); |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + private static function getFormMap(){ |
|
| 142 | + $resultArray = array(); |
|
| 143 | + foreach($_POST as $key=>$v) { |
|
| 144 | + $resultArray[$key] = $v ; |
|
| 145 | + } |
|
| 146 | + return $resultArray ; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + private static function startsWith($haystack, $needle) { |
|
| 150 | + return $needle === "" || strpos($haystack, $needle) === 0; |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + private static function endWith($haystack, $needle) { |
|
| 154 | + $length = strlen($needle); |
|
| 155 | + if($length == 0) |
|
| 156 | + { |
|
| 157 | + return true; |
|
| 158 | + } |
|
| 159 | + return (substr($haystack, -$length) === $needle); |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + private static function checkTimestamp(){ |
|
| 163 | + $ts = $_POST['timestamp']; |
|
| 164 | + if($ts){ |
|
| 165 | + $clientTimestamp = strtotime($ts); |
|
| 166 | + $current = $_SERVER['REQUEST_TIME']; |
|
| 167 | + return ($current - $clientTimestamp) <= 5*60*1000; |
|
| 168 | + }else{ |
|
| 169 | + return false; |
|
| 170 | + } |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + private static function getParamStrFromMap($params){ |
|
| 174 | + ksort($params); |
|
| 175 | + $stringToBeSigned = ""; |
|
| 176 | + foreach ($params as $k => $v) |
|
| 177 | + { |
|
| 178 | + if(strcmp("sign", $k) != 0) |
|
| 179 | + { |
|
| 180 | + $stringToBeSigned .= "$k$v"; |
|
| 181 | + } |
|
| 182 | + } |
|
| 183 | + unset($k, $v); |
|
| 184 | + return $stringToBeSigned; |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + private static function sign($params,$body,$secret){ |
|
| 188 | + ksort($params); |
|
| 189 | + |
|
| 190 | + $stringToBeSigned = $secret; |
|
| 191 | + $stringToBeSigned .= self::getParamStrFromMap($params); |
|
| 192 | + |
|
| 193 | + if($body) |
|
| 194 | + $stringToBeSigned .= $body; |
|
| 195 | + $stringToBeSigned .= $secret; |
|
| 196 | + return strtoupper(md5($stringToBeSigned)); |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + protected static function logCommunicationError($remoteSign, $localSign, $paramStr, $body) |
|
| 200 | + { |
|
| 201 | + $localIp = isset($_SERVER["SERVER_ADDR"]) ? $_SERVER["SERVER_ADDR"] : "CLI"; |
|
| 202 | + $logger = new TopLogger; |
|
| 203 | + $logger->conf["log_file"] = rtrim(TOP_SDK_WORK_DIR, '\\/') . '/' . "logs/top_comm_err_". date("Y-m-d") . ".log"; |
|
| 204 | + $logger->conf["separator"] = "^_^"; |
|
| 205 | + $logData = array( |
|
| 206 | + "checkTopSign error" , |
|
| 207 | + "remoteSign=".$remoteSign , |
|
| 208 | + "localSign=".$localSign , |
|
| 209 | + "paramStr=".$paramStr , |
|
| 210 | + "body=".$body |
|
| 211 | + ); |
|
| 212 | + $logger->log($logData); |
|
| 213 | + } |
|
| 214 | + private static function clear_blank($str, $glue='') |
|
| 215 | + { |
|
| 216 | + $replace = array(" ", "\r", "\n", "\t"); return str_replace($replace, $glue, $str); |
|
| 217 | + } |
|
| 218 | 218 | } |
| 219 | 219 | ?> |
| 220 | 220 | \ No newline at end of file |
@@ -1,377 +1,377 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | class TopClient |
| 3 | 3 | { |
| 4 | - public $appkey; |
|
| 5 | - |
|
| 6 | - public $secretKey; |
|
| 7 | - |
|
| 8 | - public $gatewayUrl = "http://gw.api.taobao.com/router/rest"; |
|
| 9 | - |
|
| 10 | - public $format = "xml"; |
|
| 11 | - |
|
| 12 | - public $connectTimeout; |
|
| 13 | - |
|
| 14 | - public $readTimeout; |
|
| 15 | - |
|
| 16 | - /** 是否打开入参check**/ |
|
| 17 | - public $checkRequest = true; |
|
| 18 | - |
|
| 19 | - protected $signMethod = "md5"; |
|
| 20 | - |
|
| 21 | - protected $apiVersion = "2.0"; |
|
| 22 | - |
|
| 23 | - protected $sdkVersion = "top-sdk-php-20180326"; |
|
| 24 | - |
|
| 25 | - public function getAppkey() |
|
| 26 | - { |
|
| 27 | - return $this->appkey; |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - public function __construct($appkey = "",$secretKey = ""){ |
|
| 31 | - $this->appkey = $appkey; |
|
| 32 | - $this->secretKey = $secretKey ; |
|
| 33 | - } |
|
| 34 | - |
|
| 35 | - protected function generateSign($params) |
|
| 36 | - { |
|
| 37 | - ksort($params); |
|
| 38 | - |
|
| 39 | - $stringToBeSigned = $this->secretKey; |
|
| 40 | - foreach ($params as $k => $v) |
|
| 41 | - { |
|
| 42 | - if(!is_array($v) && "@" != substr($v, 0, 1)) |
|
| 43 | - { |
|
| 44 | - $stringToBeSigned .= "$k$v"; |
|
| 45 | - } |
|
| 46 | - } |
|
| 47 | - unset($k, $v); |
|
| 48 | - $stringToBeSigned .= $this->secretKey; |
|
| 49 | - |
|
| 50 | - return strtoupper(md5($stringToBeSigned)); |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - public function curl($url, $postFields = null) |
|
| 54 | - { |
|
| 55 | - $ch = curl_init(); |
|
| 56 | - curl_setopt($ch, CURLOPT_URL, $url); |
|
| 57 | - curl_setopt($ch, CURLOPT_FAILONERROR, false); |
|
| 58 | - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
| 59 | - if ($this->readTimeout) { |
|
| 60 | - curl_setopt($ch, CURLOPT_TIMEOUT, $this->readTimeout); |
|
| 61 | - } |
|
| 62 | - if ($this->connectTimeout) { |
|
| 63 | - curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout); |
|
| 64 | - } |
|
| 65 | - curl_setopt ( $ch, CURLOPT_USERAGENT, "top-sdk-php" ); |
|
| 66 | - //https 请求 |
|
| 67 | - if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) { |
|
| 68 | - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
|
| 69 | - curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - if (is_array($postFields) && 0 < count($postFields)) |
|
| 73 | - { |
|
| 74 | - $postBodyString = ""; |
|
| 75 | - $postMultipart = false; |
|
| 76 | - foreach ($postFields as $k => $v) |
|
| 77 | - { |
|
| 78 | - if("@" != substr($v, 0, 1))//判断是不是文件上传 |
|
| 79 | - { |
|
| 80 | - $postBodyString .= "$k=" . urlencode($v) . "&"; |
|
| 81 | - } |
|
| 82 | - else//文件上传用multipart/form-data,否则用www-form-urlencoded |
|
| 83 | - { |
|
| 84 | - $postMultipart = true; |
|
| 85 | - if(class_exists('\CURLFile')){ |
|
| 86 | - $postFields[$k] = new \CURLFile(substr($v, 1)); |
|
| 87 | - } |
|
| 88 | - } |
|
| 89 | - } |
|
| 90 | - unset($k, $v); |
|
| 91 | - curl_setopt($ch, CURLOPT_POST, true); |
|
| 92 | - if ($postMultipart) |
|
| 93 | - { |
|
| 94 | - if (class_exists('\CURLFile')) { |
|
| 95 | - curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true); |
|
| 96 | - } else { |
|
| 97 | - if (defined('CURLOPT_SAFE_UPLOAD')) { |
|
| 98 | - curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false); |
|
| 99 | - } |
|
| 100 | - } |
|
| 101 | - curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); |
|
| 102 | - } |
|
| 103 | - else |
|
| 104 | - { |
|
| 105 | - $header = array("content-type: application/x-www-form-urlencoded; charset=UTF-8"); |
|
| 106 | - curl_setopt($ch,CURLOPT_HTTPHEADER,$header); |
|
| 107 | - curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1)); |
|
| 108 | - } |
|
| 109 | - } |
|
| 110 | - $reponse = curl_exec($ch); |
|
| 4 | + public $appkey; |
|
| 5 | + |
|
| 6 | + public $secretKey; |
|
| 7 | + |
|
| 8 | + public $gatewayUrl = "http://gw.api.taobao.com/router/rest"; |
|
| 9 | + |
|
| 10 | + public $format = "xml"; |
|
| 11 | + |
|
| 12 | + public $connectTimeout; |
|
| 13 | + |
|
| 14 | + public $readTimeout; |
|
| 15 | + |
|
| 16 | + /** 是否打开入参check**/ |
|
| 17 | + public $checkRequest = true; |
|
| 18 | + |
|
| 19 | + protected $signMethod = "md5"; |
|
| 20 | + |
|
| 21 | + protected $apiVersion = "2.0"; |
|
| 22 | + |
|
| 23 | + protected $sdkVersion = "top-sdk-php-20180326"; |
|
| 24 | + |
|
| 25 | + public function getAppkey() |
|
| 26 | + { |
|
| 27 | + return $this->appkey; |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + public function __construct($appkey = "",$secretKey = ""){ |
|
| 31 | + $this->appkey = $appkey; |
|
| 32 | + $this->secretKey = $secretKey ; |
|
| 33 | + } |
|
| 34 | + |
|
| 35 | + protected function generateSign($params) |
|
| 36 | + { |
|
| 37 | + ksort($params); |
|
| 38 | + |
|
| 39 | + $stringToBeSigned = $this->secretKey; |
|
| 40 | + foreach ($params as $k => $v) |
|
| 41 | + { |
|
| 42 | + if(!is_array($v) && "@" != substr($v, 0, 1)) |
|
| 43 | + { |
|
| 44 | + $stringToBeSigned .= "$k$v"; |
|
| 45 | + } |
|
| 46 | + } |
|
| 47 | + unset($k, $v); |
|
| 48 | + $stringToBeSigned .= $this->secretKey; |
|
| 49 | + |
|
| 50 | + return strtoupper(md5($stringToBeSigned)); |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + public function curl($url, $postFields = null) |
|
| 54 | + { |
|
| 55 | + $ch = curl_init(); |
|
| 56 | + curl_setopt($ch, CURLOPT_URL, $url); |
|
| 57 | + curl_setopt($ch, CURLOPT_FAILONERROR, false); |
|
| 58 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
| 59 | + if ($this->readTimeout) { |
|
| 60 | + curl_setopt($ch, CURLOPT_TIMEOUT, $this->readTimeout); |
|
| 61 | + } |
|
| 62 | + if ($this->connectTimeout) { |
|
| 63 | + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout); |
|
| 64 | + } |
|
| 65 | + curl_setopt ( $ch, CURLOPT_USERAGENT, "top-sdk-php" ); |
|
| 66 | + //https 请求 |
|
| 67 | + if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) { |
|
| 68 | + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
|
| 69 | + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + if (is_array($postFields) && 0 < count($postFields)) |
|
| 73 | + { |
|
| 74 | + $postBodyString = ""; |
|
| 75 | + $postMultipart = false; |
|
| 76 | + foreach ($postFields as $k => $v) |
|
| 77 | + { |
|
| 78 | + if("@" != substr($v, 0, 1))//判断是不是文件上传 |
|
| 79 | + { |
|
| 80 | + $postBodyString .= "$k=" . urlencode($v) . "&"; |
|
| 81 | + } |
|
| 82 | + else//文件上传用multipart/form-data,否则用www-form-urlencoded |
|
| 83 | + { |
|
| 84 | + $postMultipart = true; |
|
| 85 | + if(class_exists('\CURLFile')){ |
|
| 86 | + $postFields[$k] = new \CURLFile(substr($v, 1)); |
|
| 87 | + } |
|
| 88 | + } |
|
| 89 | + } |
|
| 90 | + unset($k, $v); |
|
| 91 | + curl_setopt($ch, CURLOPT_POST, true); |
|
| 92 | + if ($postMultipart) |
|
| 93 | + { |
|
| 94 | + if (class_exists('\CURLFile')) { |
|
| 95 | + curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true); |
|
| 96 | + } else { |
|
| 97 | + if (defined('CURLOPT_SAFE_UPLOAD')) { |
|
| 98 | + curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false); |
|
| 99 | + } |
|
| 100 | + } |
|
| 101 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); |
|
| 102 | + } |
|
| 103 | + else |
|
| 104 | + { |
|
| 105 | + $header = array("content-type: application/x-www-form-urlencoded; charset=UTF-8"); |
|
| 106 | + curl_setopt($ch,CURLOPT_HTTPHEADER,$header); |
|
| 107 | + curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1)); |
|
| 108 | + } |
|
| 109 | + } |
|
| 110 | + $reponse = curl_exec($ch); |
|
| 111 | 111 | |
| 112 | - if (curl_errno($ch)) |
|
| 113 | - { |
|
| 114 | - throw new Exception(curl_error($ch),0); |
|
| 115 | - } |
|
| 116 | - else |
|
| 117 | - { |
|
| 118 | - $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
|
| 119 | - if (200 !== $httpStatusCode) |
|
| 120 | - { |
|
| 121 | - throw new Exception($reponse,$httpStatusCode); |
|
| 122 | - } |
|
| 123 | - } |
|
| 124 | - curl_close($ch); |
|
| 125 | - return $reponse; |
|
| 126 | - } |
|
| 127 | - public function curl_with_memory_file($url, $postFields = null, $fileFields = null) |
|
| 128 | - { |
|
| 129 | - $ch = curl_init(); |
|
| 130 | - curl_setopt($ch, CURLOPT_URL, $url); |
|
| 131 | - curl_setopt($ch, CURLOPT_FAILONERROR, false); |
|
| 132 | - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
| 133 | - if ($this->readTimeout) { |
|
| 134 | - curl_setopt($ch, CURLOPT_TIMEOUT, $this->readTimeout); |
|
| 135 | - } |
|
| 136 | - if ($this->connectTimeout) { |
|
| 137 | - curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout); |
|
| 138 | - } |
|
| 139 | - curl_setopt ( $ch, CURLOPT_USERAGENT, "top-sdk-php" ); |
|
| 140 | - //https 请求 |
|
| 141 | - if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) { |
|
| 142 | - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
|
| 143 | - curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); |
|
| 144 | - } |
|
| 145 | - //生成分隔符 |
|
| 146 | - $delimiter = '-------------' . uniqid(); |
|
| 147 | - //先将post的普通数据生成主体字符串 |
|
| 148 | - $data = ''; |
|
| 149 | - if($postFields != null){ |
|
| 150 | - foreach ($postFields as $name => $content) { |
|
| 151 | - $data .= "--" . $delimiter . "\r\n"; |
|
| 152 | - $data .= 'Content-Disposition: form-data; name="' . $name . '"'; |
|
| 153 | - //multipart/form-data 不需要urlencode,参见 http:stackoverflow.com/questions/6603928/should-i-url-encode-post-data |
|
| 154 | - $data .= "\r\n\r\n" . $content . "\r\n"; |
|
| 155 | - } |
|
| 156 | - unset($name,$content); |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - //将上传的文件生成主体字符串 |
|
| 160 | - if($fileFields != null){ |
|
| 161 | - foreach ($fileFields as $name => $file) { |
|
| 162 | - $data .= "--" . $delimiter . "\r\n"; |
|
| 163 | - $data .= 'Content-Disposition: form-data; name="' . $name . '"; filename="' . $file['name'] . "\" \r\n"; |
|
| 164 | - $data .= 'Content-Type: ' . $file['type'] . "\r\n\r\n";//多了个文档类型 |
|
| 165 | - |
|
| 166 | - $data .= $file['content'] . "\r\n"; |
|
| 167 | - } |
|
| 168 | - unset($name,$file); |
|
| 169 | - } |
|
| 170 | - //主体结束的分隔符 |
|
| 171 | - $data .= "--" . $delimiter . "--"; |
|
| 172 | - |
|
| 173 | - curl_setopt($ch, CURLOPT_POST, true); |
|
| 174 | - curl_setopt($ch, CURLOPT_HTTPHEADER , array( |
|
| 175 | - 'Content-Type: multipart/form-data; boundary=' . $delimiter, |
|
| 176 | - 'Content-Length: ' . strlen($data)) |
|
| 177 | - ); |
|
| 178 | - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
|
| 179 | - curl_setopt($ch, CURLOPT_POSTFIELDS, $data); |
|
| 180 | - |
|
| 181 | - $reponse = curl_exec($ch); |
|
| 182 | - unset($data); |
|
| 183 | - |
|
| 184 | - if (curl_errno($ch)) |
|
| 185 | - { |
|
| 186 | - throw new Exception(curl_error($ch),0); |
|
| 187 | - } |
|
| 188 | - else |
|
| 189 | - { |
|
| 190 | - $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
|
| 191 | - if (200 !== $httpStatusCode) |
|
| 192 | - { |
|
| 193 | - throw new Exception($reponse,$httpStatusCode); |
|
| 194 | - } |
|
| 195 | - } |
|
| 196 | - curl_close($ch); |
|
| 197 | - return $reponse; |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - protected function logCommunicationError($apiName, $requestUrl, $errorCode, $responseTxt) |
|
| 201 | - { |
|
| 202 | - $localIp = isset($_SERVER["SERVER_ADDR"]) ? $_SERVER["SERVER_ADDR"] : "CLI"; |
|
| 203 | - $logger = new TopLogger; |
|
| 204 | - $logger->conf["log_file"] = rtrim(TOP_SDK_WORK_DIR, '\\/') . '/' . "logs/top_comm_err_" . $this->appkey . "_" . date("Y-m-d") . ".log"; |
|
| 205 | - $logger->conf["separator"] = "^_^"; |
|
| 206 | - $logData = array( |
|
| 207 | - date("Y-m-d H:i:s"), |
|
| 208 | - $apiName, |
|
| 209 | - $this->appkey, |
|
| 210 | - $localIp, |
|
| 211 | - PHP_OS, |
|
| 212 | - $this->sdkVersion, |
|
| 213 | - $requestUrl, |
|
| 214 | - $errorCode, |
|
| 215 | - str_replace("\n","",$responseTxt) |
|
| 216 | - ); |
|
| 217 | - $logger->log($logData); |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - public function execute($request, $session = null,$bestUrl = null) |
|
| 221 | - { |
|
| 222 | - $result = new ResultSet(); |
|
| 223 | - if($this->checkRequest) { |
|
| 224 | - try { |
|
| 225 | - $request->check(); |
|
| 226 | - } catch (Exception $e) { |
|
| 227 | - |
|
| 228 | - $result->code = $e->getCode(); |
|
| 229 | - $result->msg = $e->getMessage(); |
|
| 230 | - return $result; |
|
| 231 | - } |
|
| 232 | - } |
|
| 233 | - //组装系统参数 |
|
| 234 | - $sysParams["app_key"] = $this->appkey; |
|
| 235 | - $sysParams["v"] = $this->apiVersion; |
|
| 236 | - $sysParams["format"] = $this->format; |
|
| 237 | - $sysParams["sign_method"] = $this->signMethod; |
|
| 238 | - $sysParams["method"] = $request->getApiMethodName(); |
|
| 239 | - $sysParams["timestamp"] = date("Y-m-d H:i:s"); |
|
| 240 | - if (null != $session) |
|
| 241 | - { |
|
| 242 | - $sysParams["session"] = $session; |
|
| 243 | - } |
|
| 244 | - $apiParams = array(); |
|
| 245 | - //获取业务参数 |
|
| 246 | - $apiParams = $request->getApiParas(); |
|
| 247 | - |
|
| 248 | - |
|
| 249 | - //系统参数放入GET请求串 |
|
| 250 | - if($bestUrl){ |
|
| 251 | - $requestUrl = $bestUrl."?"; |
|
| 252 | - $sysParams["partner_id"] = $this->getClusterTag(); |
|
| 253 | - }else{ |
|
| 254 | - $requestUrl = $this->gatewayUrl."?"; |
|
| 255 | - $sysParams["partner_id"] = $this->sdkVersion; |
|
| 256 | - } |
|
| 257 | - //签名 |
|
| 258 | - $sysParams["sign"] = $this->generateSign(array_merge($apiParams, $sysParams)); |
|
| 259 | - |
|
| 260 | - foreach ($sysParams as $sysParamKey => $sysParamValue) |
|
| 261 | - { |
|
| 262 | - // if(strcmp($sysParamKey,"timestamp") != 0) |
|
| 263 | - $requestUrl .= "$sysParamKey=" . urlencode($sysParamValue) . "&"; |
|
| 264 | - } |
|
| 265 | - |
|
| 266 | - $fileFields = array(); |
|
| 267 | - foreach ($apiParams as $key => $value) { |
|
| 268 | - if(is_array($value) && array_key_exists('type',$value) && array_key_exists('content',$value) ){ |
|
| 269 | - $value['name'] = $key; |
|
| 270 | - $fileFields[$key] = $value; |
|
| 271 | - unset($apiParams[$key]); |
|
| 272 | - } |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - // $requestUrl .= "timestamp=" . urlencode($sysParams["timestamp"]) . "&"; |
|
| 276 | - $requestUrl = substr($requestUrl, 0, -1); |
|
| 277 | - |
|
| 278 | - //发起HTTP请求 |
|
| 279 | - try |
|
| 280 | - { |
|
| 281 | - if(count($fileFields) > 0){ |
|
| 282 | - $resp = $this->curl_with_memory_file($requestUrl, $apiParams, $fileFields); |
|
| 283 | - }else{ |
|
| 284 | - $resp = $this->curl($requestUrl, $apiParams); |
|
| 285 | - } |
|
| 286 | - } |
|
| 287 | - catch (Exception $e) |
|
| 288 | - { |
|
| 289 | - $this->logCommunicationError($sysParams["method"],$requestUrl,"HTTP_ERROR_" . $e->getCode(),$e->getMessage()); |
|
| 290 | - $result->code = $e->getCode(); |
|
| 291 | - $result->msg = $e->getMessage(); |
|
| 292 | - return $result; |
|
| 293 | - } |
|
| 294 | - |
|
| 295 | - unset($apiParams); |
|
| 296 | - unset($fileFields); |
|
| 297 | - //解析TOP返回结果 |
|
| 298 | - $respWellFormed = false; |
|
| 299 | - if ("json" == $this->format) |
|
| 300 | - { |
|
| 301 | - $respObject = json_decode($resp); |
|
| 302 | - if (null !== $respObject) |
|
| 303 | - { |
|
| 304 | - $respWellFormed = true; |
|
| 305 | - foreach ($respObject as $propKey => $propValue) |
|
| 306 | - { |
|
| 307 | - $respObject = $propValue; |
|
| 308 | - } |
|
| 309 | - } |
|
| 310 | - } |
|
| 311 | - else if("xml" == $this->format) |
|
| 312 | - { |
|
| 313 | - $respObject = @simplexml_load_string($resp); |
|
| 314 | - if (false !== $respObject) |
|
| 315 | - { |
|
| 316 | - $respWellFormed = true; |
|
| 317 | - } |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - //返回的HTTP文本不是标准JSON或者XML,记下错误日志 |
|
| 321 | - if (false === $respWellFormed) |
|
| 322 | - { |
|
| 323 | - $this->logCommunicationError($sysParams["method"],$requestUrl,"HTTP_RESPONSE_NOT_WELL_FORMED",$resp); |
|
| 324 | - $result->code = 0; |
|
| 325 | - $result->msg = "HTTP_RESPONSE_NOT_WELL_FORMED"; |
|
| 326 | - return $result; |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - //如果TOP返回了错误码,记录到业务错误日志中 |
|
| 330 | - if (isset($respObject->code)) |
|
| 331 | - { |
|
| 332 | - $logger = new TopLogger; |
|
| 333 | - $logger->conf["log_file"] = rtrim(TOP_SDK_WORK_DIR, '\\/') . '/' . "logs/top_biz_err_" . $this->appkey . "_" . date("Y-m-d") . ".log"; |
|
| 334 | - $logger->log(array( |
|
| 335 | - date("Y-m-d H:i:s"), |
|
| 336 | - $resp |
|
| 337 | - )); |
|
| 338 | - } |
|
| 339 | - return $respObject; |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - public function exec($paramsArray) |
|
| 343 | - { |
|
| 344 | - if (!isset($paramsArray["method"])) |
|
| 345 | - { |
|
| 346 | - trigger_error("No api name passed"); |
|
| 347 | - } |
|
| 348 | - $inflector = new LtInflector; |
|
| 349 | - $inflector->conf["separator"] = "."; |
|
| 350 | - $requestClassName = ucfirst($inflector->camelize(substr($paramsArray["method"], 7))) . "Request"; |
|
| 351 | - if (!class_exists($requestClassName)) |
|
| 352 | - { |
|
| 353 | - trigger_error("No such api: " . $paramsArray["method"]); |
|
| 354 | - } |
|
| 355 | - |
|
| 356 | - $session = isset($paramsArray["session"]) ? $paramsArray["session"] : null; |
|
| 357 | - |
|
| 358 | - $req = new $requestClassName; |
|
| 359 | - foreach($paramsArray as $paraKey => $paraValue) |
|
| 360 | - { |
|
| 361 | - $inflector->conf["separator"] = "_"; |
|
| 362 | - $setterMethodName = $inflector->camelize($paraKey); |
|
| 363 | - $inflector->conf["separator"] = "."; |
|
| 364 | - $setterMethodName = "set" . $inflector->camelize($setterMethodName); |
|
| 365 | - if (method_exists($req, $setterMethodName)) |
|
| 366 | - { |
|
| 367 | - $req->$setterMethodName($paraValue); |
|
| 368 | - } |
|
| 369 | - } |
|
| 370 | - return $this->execute($req, $session); |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - private function getClusterTag() |
|
| 112 | + if (curl_errno($ch)) |
|
| 113 | + { |
|
| 114 | + throw new Exception(curl_error($ch),0); |
|
| 115 | + } |
|
| 116 | + else |
|
| 117 | + { |
|
| 118 | + $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
|
| 119 | + if (200 !== $httpStatusCode) |
|
| 120 | + { |
|
| 121 | + throw new Exception($reponse,$httpStatusCode); |
|
| 122 | + } |
|
| 123 | + } |
|
| 124 | + curl_close($ch); |
|
| 125 | + return $reponse; |
|
| 126 | + } |
|
| 127 | + public function curl_with_memory_file($url, $postFields = null, $fileFields = null) |
|
| 128 | + { |
|
| 129 | + $ch = curl_init(); |
|
| 130 | + curl_setopt($ch, CURLOPT_URL, $url); |
|
| 131 | + curl_setopt($ch, CURLOPT_FAILONERROR, false); |
|
| 132 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
| 133 | + if ($this->readTimeout) { |
|
| 134 | + curl_setopt($ch, CURLOPT_TIMEOUT, $this->readTimeout); |
|
| 135 | + } |
|
| 136 | + if ($this->connectTimeout) { |
|
| 137 | + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout); |
|
| 138 | + } |
|
| 139 | + curl_setopt ( $ch, CURLOPT_USERAGENT, "top-sdk-php" ); |
|
| 140 | + //https 请求 |
|
| 141 | + if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) { |
|
| 142 | + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
|
| 143 | + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); |
|
| 144 | + } |
|
| 145 | + //生成分隔符 |
|
| 146 | + $delimiter = '-------------' . uniqid(); |
|
| 147 | + //先将post的普通数据生成主体字符串 |
|
| 148 | + $data = ''; |
|
| 149 | + if($postFields != null){ |
|
| 150 | + foreach ($postFields as $name => $content) { |
|
| 151 | + $data .= "--" . $delimiter . "\r\n"; |
|
| 152 | + $data .= 'Content-Disposition: form-data; name="' . $name . '"'; |
|
| 153 | + //multipart/form-data 不需要urlencode,参见 http:stackoverflow.com/questions/6603928/should-i-url-encode-post-data |
|
| 154 | + $data .= "\r\n\r\n" . $content . "\r\n"; |
|
| 155 | + } |
|
| 156 | + unset($name,$content); |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + //将上传的文件生成主体字符串 |
|
| 160 | + if($fileFields != null){ |
|
| 161 | + foreach ($fileFields as $name => $file) { |
|
| 162 | + $data .= "--" . $delimiter . "\r\n"; |
|
| 163 | + $data .= 'Content-Disposition: form-data; name="' . $name . '"; filename="' . $file['name'] . "\" \r\n"; |
|
| 164 | + $data .= 'Content-Type: ' . $file['type'] . "\r\n\r\n";//多了个文档类型 |
|
| 165 | + |
|
| 166 | + $data .= $file['content'] . "\r\n"; |
|
| 167 | + } |
|
| 168 | + unset($name,$file); |
|
| 169 | + } |
|
| 170 | + //主体结束的分隔符 |
|
| 171 | + $data .= "--" . $delimiter . "--"; |
|
| 172 | + |
|
| 173 | + curl_setopt($ch, CURLOPT_POST, true); |
|
| 174 | + curl_setopt($ch, CURLOPT_HTTPHEADER , array( |
|
| 175 | + 'Content-Type: multipart/form-data; boundary=' . $delimiter, |
|
| 176 | + 'Content-Length: ' . strlen($data)) |
|
| 177 | + ); |
|
| 178 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
|
| 179 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $data); |
|
| 180 | + |
|
| 181 | + $reponse = curl_exec($ch); |
|
| 182 | + unset($data); |
|
| 183 | + |
|
| 184 | + if (curl_errno($ch)) |
|
| 185 | + { |
|
| 186 | + throw new Exception(curl_error($ch),0); |
|
| 187 | + } |
|
| 188 | + else |
|
| 189 | + { |
|
| 190 | + $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
|
| 191 | + if (200 !== $httpStatusCode) |
|
| 192 | + { |
|
| 193 | + throw new Exception($reponse,$httpStatusCode); |
|
| 194 | + } |
|
| 195 | + } |
|
| 196 | + curl_close($ch); |
|
| 197 | + return $reponse; |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + protected function logCommunicationError($apiName, $requestUrl, $errorCode, $responseTxt) |
|
| 201 | + { |
|
| 202 | + $localIp = isset($_SERVER["SERVER_ADDR"]) ? $_SERVER["SERVER_ADDR"] : "CLI"; |
|
| 203 | + $logger = new TopLogger; |
|
| 204 | + $logger->conf["log_file"] = rtrim(TOP_SDK_WORK_DIR, '\\/') . '/' . "logs/top_comm_err_" . $this->appkey . "_" . date("Y-m-d") . ".log"; |
|
| 205 | + $logger->conf["separator"] = "^_^"; |
|
| 206 | + $logData = array( |
|
| 207 | + date("Y-m-d H:i:s"), |
|
| 208 | + $apiName, |
|
| 209 | + $this->appkey, |
|
| 210 | + $localIp, |
|
| 211 | + PHP_OS, |
|
| 212 | + $this->sdkVersion, |
|
| 213 | + $requestUrl, |
|
| 214 | + $errorCode, |
|
| 215 | + str_replace("\n","",$responseTxt) |
|
| 216 | + ); |
|
| 217 | + $logger->log($logData); |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + public function execute($request, $session = null,$bestUrl = null) |
|
| 221 | + { |
|
| 222 | + $result = new ResultSet(); |
|
| 223 | + if($this->checkRequest) { |
|
| 224 | + try { |
|
| 225 | + $request->check(); |
|
| 226 | + } catch (Exception $e) { |
|
| 227 | + |
|
| 228 | + $result->code = $e->getCode(); |
|
| 229 | + $result->msg = $e->getMessage(); |
|
| 230 | + return $result; |
|
| 231 | + } |
|
| 232 | + } |
|
| 233 | + //组装系统参数 |
|
| 234 | + $sysParams["app_key"] = $this->appkey; |
|
| 235 | + $sysParams["v"] = $this->apiVersion; |
|
| 236 | + $sysParams["format"] = $this->format; |
|
| 237 | + $sysParams["sign_method"] = $this->signMethod; |
|
| 238 | + $sysParams["method"] = $request->getApiMethodName(); |
|
| 239 | + $sysParams["timestamp"] = date("Y-m-d H:i:s"); |
|
| 240 | + if (null != $session) |
|
| 241 | + { |
|
| 242 | + $sysParams["session"] = $session; |
|
| 243 | + } |
|
| 244 | + $apiParams = array(); |
|
| 245 | + //获取业务参数 |
|
| 246 | + $apiParams = $request->getApiParas(); |
|
| 247 | + |
|
| 248 | + |
|
| 249 | + //系统参数放入GET请求串 |
|
| 250 | + if($bestUrl){ |
|
| 251 | + $requestUrl = $bestUrl."?"; |
|
| 252 | + $sysParams["partner_id"] = $this->getClusterTag(); |
|
| 253 | + }else{ |
|
| 254 | + $requestUrl = $this->gatewayUrl."?"; |
|
| 255 | + $sysParams["partner_id"] = $this->sdkVersion; |
|
| 256 | + } |
|
| 257 | + //签名 |
|
| 258 | + $sysParams["sign"] = $this->generateSign(array_merge($apiParams, $sysParams)); |
|
| 259 | + |
|
| 260 | + foreach ($sysParams as $sysParamKey => $sysParamValue) |
|
| 261 | + { |
|
| 262 | + // if(strcmp($sysParamKey,"timestamp") != 0) |
|
| 263 | + $requestUrl .= "$sysParamKey=" . urlencode($sysParamValue) . "&"; |
|
| 264 | + } |
|
| 265 | + |
|
| 266 | + $fileFields = array(); |
|
| 267 | + foreach ($apiParams as $key => $value) { |
|
| 268 | + if(is_array($value) && array_key_exists('type',$value) && array_key_exists('content',$value) ){ |
|
| 269 | + $value['name'] = $key; |
|
| 270 | + $fileFields[$key] = $value; |
|
| 271 | + unset($apiParams[$key]); |
|
| 272 | + } |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + // $requestUrl .= "timestamp=" . urlencode($sysParams["timestamp"]) . "&"; |
|
| 276 | + $requestUrl = substr($requestUrl, 0, -1); |
|
| 277 | + |
|
| 278 | + //发起HTTP请求 |
|
| 279 | + try |
|
| 280 | + { |
|
| 281 | + if(count($fileFields) > 0){ |
|
| 282 | + $resp = $this->curl_with_memory_file($requestUrl, $apiParams, $fileFields); |
|
| 283 | + }else{ |
|
| 284 | + $resp = $this->curl($requestUrl, $apiParams); |
|
| 285 | + } |
|
| 286 | + } |
|
| 287 | + catch (Exception $e) |
|
| 288 | + { |
|
| 289 | + $this->logCommunicationError($sysParams["method"],$requestUrl,"HTTP_ERROR_" . $e->getCode(),$e->getMessage()); |
|
| 290 | + $result->code = $e->getCode(); |
|
| 291 | + $result->msg = $e->getMessage(); |
|
| 292 | + return $result; |
|
| 293 | + } |
|
| 294 | + |
|
| 295 | + unset($apiParams); |
|
| 296 | + unset($fileFields); |
|
| 297 | + //解析TOP返回结果 |
|
| 298 | + $respWellFormed = false; |
|
| 299 | + if ("json" == $this->format) |
|
| 300 | + { |
|
| 301 | + $respObject = json_decode($resp); |
|
| 302 | + if (null !== $respObject) |
|
| 303 | + { |
|
| 304 | + $respWellFormed = true; |
|
| 305 | + foreach ($respObject as $propKey => $propValue) |
|
| 306 | + { |
|
| 307 | + $respObject = $propValue; |
|
| 308 | + } |
|
| 309 | + } |
|
| 310 | + } |
|
| 311 | + else if("xml" == $this->format) |
|
| 312 | + { |
|
| 313 | + $respObject = @simplexml_load_string($resp); |
|
| 314 | + if (false !== $respObject) |
|
| 315 | + { |
|
| 316 | + $respWellFormed = true; |
|
| 317 | + } |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + //返回的HTTP文本不是标准JSON或者XML,记下错误日志 |
|
| 321 | + if (false === $respWellFormed) |
|
| 322 | + { |
|
| 323 | + $this->logCommunicationError($sysParams["method"],$requestUrl,"HTTP_RESPONSE_NOT_WELL_FORMED",$resp); |
|
| 324 | + $result->code = 0; |
|
| 325 | + $result->msg = "HTTP_RESPONSE_NOT_WELL_FORMED"; |
|
| 326 | + return $result; |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + //如果TOP返回了错误码,记录到业务错误日志中 |
|
| 330 | + if (isset($respObject->code)) |
|
| 331 | + { |
|
| 332 | + $logger = new TopLogger; |
|
| 333 | + $logger->conf["log_file"] = rtrim(TOP_SDK_WORK_DIR, '\\/') . '/' . "logs/top_biz_err_" . $this->appkey . "_" . date("Y-m-d") . ".log"; |
|
| 334 | + $logger->log(array( |
|
| 335 | + date("Y-m-d H:i:s"), |
|
| 336 | + $resp |
|
| 337 | + )); |
|
| 338 | + } |
|
| 339 | + return $respObject; |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + public function exec($paramsArray) |
|
| 343 | + { |
|
| 344 | + if (!isset($paramsArray["method"])) |
|
| 345 | + { |
|
| 346 | + trigger_error("No api name passed"); |
|
| 347 | + } |
|
| 348 | + $inflector = new LtInflector; |
|
| 349 | + $inflector->conf["separator"] = "."; |
|
| 350 | + $requestClassName = ucfirst($inflector->camelize(substr($paramsArray["method"], 7))) . "Request"; |
|
| 351 | + if (!class_exists($requestClassName)) |
|
| 352 | + { |
|
| 353 | + trigger_error("No such api: " . $paramsArray["method"]); |
|
| 354 | + } |
|
| 355 | + |
|
| 356 | + $session = isset($paramsArray["session"]) ? $paramsArray["session"] : null; |
|
| 357 | + |
|
| 358 | + $req = new $requestClassName; |
|
| 359 | + foreach($paramsArray as $paraKey => $paraValue) |
|
| 360 | + { |
|
| 361 | + $inflector->conf["separator"] = "_"; |
|
| 362 | + $setterMethodName = $inflector->camelize($paraKey); |
|
| 363 | + $inflector->conf["separator"] = "."; |
|
| 364 | + $setterMethodName = "set" . $inflector->camelize($setterMethodName); |
|
| 365 | + if (method_exists($req, $setterMethodName)) |
|
| 366 | + { |
|
| 367 | + $req->$setterMethodName($paraValue); |
|
| 368 | + } |
|
| 369 | + } |
|
| 370 | + return $this->execute($req, $session); |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + private function getClusterTag() |
|
| 374 | 374 | { |
| 375 | - return substr($this->sdkVersion,0,11)."-cluster".substr($this->sdkVersion,11); |
|
| 375 | + return substr($this->sdkVersion,0,11)."-cluster".substr($this->sdkVersion,11); |
|
| 376 | 376 | } |
| 377 | 377 | } |