· 6 years ago · Jun 14, 2019, 12:34 PM
1function responseEncoderDecoder( $string, $action = 'e' ) {
2 // you may change these values to your own from admin
3 $secret_key = 'Q%28bjZPgX$-vUNS';
4 $secret_iv = '9H6sa5E7h=qH=raB';
5
6 $output = false;
7 $encrypt_method = "AES-256-CBC";
8 $key = hash( 'sha256', $secret_key );
9 $iv = substr( hash( 'sha256', $secret_iv ), 0, 16 );
10
11 if( $action == 'e' ) {
12 $output = base64_encode( openssl_encrypt( $string, $encrypt_method, $key, 0, $iv ) );
13 }
14 else if( $action == 'd' ){
15 $output = openssl_decrypt( base64_decode( $string ), $encrypt_method, $key, 0, $iv );
16 }
17 return $output;
18
19 }
20
21$router->post('/APITESTRQ', function (Illuminate\Http\Request $request) {
22 if(!empty($_POST['send'])){
23 ?>
24
25 Api teste:
26 <form action="/APITESTRQ" method="post">
27 <input type="hidden" name="send" value="true" />
28 <h1>Endpoint</h1>
29 <input type="text" placeholder="endpoint" name="endpoint" value="<?php echo $_POST['endpoint']; ?>" /><Br />
30 <h2>Corpo do Json</h2>
31 <textarea name="jsonbody" style="height: 100px;width:300px;"><?php echo $_POST['jsonbody']; ?></textarea>
32
33 <Br>
34 <input type="submit" />
35 </form>
36 <?php
37 $jsonreq = $_POST['jsonbody'];
38 echo '-------- REQUEST --------<br>';
39 echo $jsonreq;
40
41 $jsonreq = responseEncoderDecoder($jsonreq,'e');
42
43 echo '<br>-------- ENCODED ---------<br>';
44 echo $jsonreq;
45 //$jsonreq = json_encode($mockupReq);
46 $client = new \GuzzleHttp\Client(['base_uri' => 'https://prod.zarpo.com.br/miapi/']);
47
48
49 $headers = ['json' => $jsonreq, 'content-type' => 'application/json' ];
50
51 $body = $jsonreq;
52
53 $endpoint = $_POST['endpoint'];
54 $response = $client->request('PUT', $endpoint, $headers, $body);
55 echo 'HEADERS: <br>';
56 echo $response->getStatusCode(); # 200
57 echo $response->getHeaderLine('content-type'); # 'application/json; charset=utf8'
58 echo '<br> -------------- RESPONSE ----------<br>';
59 echo $response->getBody();
60 echo '<br> ---------- DECODED ---------<br>';
61
62 echo responseEncoderDecoder($response->getBody(),'d'); # '{"id": 1420053, "name": "guzzle", ...}'
63
64 }
65
66
67 return;
68
69
70});