-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcURL.php
65 lines (55 loc) · 2.24 KB
/
cURL.php
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
<?php
/**
* Simple cURL request script
*
* Test if cURL is available, send request, print response
*
* php curl.php
*/
if(!function_exists('curl_init')) {
die('cURL not available!');
}
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://example.com/api.php'); // or use https://requestb.in/ for testing purposes
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//// Require fresh connection
//curl_setopt($curl, CURLOPT_FRESH_CONNECT, true);
//// Send POST request instead of GET and transfer data
//$postData = array(
// 'name' => 'John Doe',
// 'submit' => '1'
//);
//curl_setopt($curl, CURLOPT_POST, true);
//curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($postData));
//// Use a different request method
//// Note: PHP only converts data of GET and POST requests into convenient superglobals (»$_GET« & »$_POST«)
//// Use »parse_str(file_get_contents('php://input'), $_DELETE);« to read incoming data and write it to a
//// custom superglobal
//curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
//// If the service is not able to parse the input stream, then use a regular POST request and a custom header
//// Use »$_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']« to read the incoming header variable
//curl_setopt($curl, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: PUT'));
//// Send JSON body via POST request
//$postData = array(
// 'name' => 'John Doe',
// 'submit' => '1'
//);
//curl_setopt($curl, CURLOPT_POST, true);
//curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($postData));
//curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json', 'Accept:application/json')); // send JSON and expect JSON
//// As said above, the target script needs to read `php://input`, not `$_POST`!
//// Timeout in seconds
//curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 0);
//curl_setopt($curl, CURLOPT_TIMEOUT, 10);
//// Dont verify SSL certificate (eg. self-signed cert in testsystem)
//curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
//curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$output = curl_exec($curl);
if ($output === FALSE) {
echo 'An error has occurred: ' . curl_error($curl) . PHP_EOL;
}
else {
echo $output;
}