forked from GoogleCloudPlatform/php-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.php
More file actions
121 lines (102 loc) · 3.69 KB
/
Copy pathapp.php
File metadata and controls
121 lines (102 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
/**
* Copyright 2016 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use Silex\Application;
use Silex\Provider\TwigServiceProvider;
// create the Silex application
$app = new Application();
$app->register(new TwigServiceProvider());
$app['twig.path'] = [ __DIR__ ];
$app->get('/', function () use ($app) {
/** @var Twig_Environment $twig */
$twig = $app['twig'];
return $twig->render('http.html.twig');
});
$app->post('/request/file', function () use ($app) {
/** @var Twig_Environment $twig */
$twig = $app['twig'];
# [START gae_issue_request_http_bin]
$url = 'http://httpbin.org/post?query=update';
$data = ['data' => 'this', 'data2' => 'that'];
$headers = "accept: */*\r\n" .
"Content-Type: application/x-www-form-urlencoded\r\n" .
"Custom-Header: custom-value\r\n" .
"Custom-Header-Two: custom-value-2\r\n";
$context = [
'http' => [
'method' => 'POST',
'header' => $headers,
'content' => http_build_query($data),
]
];
$context = stream_context_create($context);
$result = file_get_contents($url, false, $context);
# [END gae_issue_request_http_bin]
return $twig->render('http.html.twig', ['file_result' => $result]);
});
$app->post('/request/curl', function () use ($app) {
/** @var Twig_Environment $twig */
$twig = $app['twig'];
// make sure one of the extensions is installed
if (!function_exists('curl_init')) {
throw new \Exception('You must enable cURL or cURLite in php.ini');
}
# [START gae_issue_request_curl_request]
$url = 'http://httpbin.org/post?query=update';
$data = ['data' => 'this', 'data2' => 'that'];
$headers = [
'Accept: */*',
'Content-Type: application/x-www-form-urlencoded',
'Custom-Header: custom-value',
'Custom-Header-Two: custom-value-2'
];
// open connection
$ch = curl_init();
// set curl options
$options = [
CURLOPT_URL => $url,
CURLOPT_POST => count($data),
CURLOPT_POSTFIELDS => http_build_query($data),
CURLOPT_HTTPHEADER => $headers,
CURLOPT_RETURNTRANSFER => true,
];
curl_setopt_array($ch, $options);
// execute
$result = curl_exec($ch);
// close connection
curl_close($ch);
# [END gae_issue_request_curl_request]
return $twig->render('http.html.twig', ['curl_result' => $result]);
});
$app->post('/request/guzzle', function () use ($app) {
/** @var Twig_Environment $twig */
$twig = $app['twig'];
# [START gae_issue_request_guzzle_request]
$url = 'http://httpbin.org/post?query=update';
$data = ['data' => 'this', 'data2' => 'that'];
$headers = [
'Accept' => '*/*',
'Content-Type' => 'application/x-www-form-urlencoded',
'Custom-Header' => 'custom-value',
'Custom-Header-Two' => 'custom-value',
];
$guzzle = new GuzzleHttp\Client;
$request = new GuzzleHttp\Psr7\Request('POST', $url, $headers, http_build_query($data));
$result = $guzzle->send($request);
# [END gae_issue_request_guzzle_request]
return $twig->render('http.html.twig', ['guzzle_result' => $result->getBody()]);
});
return $app;