1$post = [
2 'teste' => $_POST['teste']
3];
4httpPost('url.com', $post);
5// function
6function httpPost($url, $data)
7{
8 $curl = curl_init($url);
9 curl_setopt($curl, CURLOPT_POST, true);
10 curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
11 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
12 $response = curl_exec($curl);
13 curl_close($curl);
14 return $response;
15}
1// Since PHP 5.4.0 you can use getallheaders function which returns all request headers as an associative array:
2
3var_dump(getallheaders());
4
5// array(8) {
6// ["Accept"]=>
7// string(63) "text/html[...]"
8// ["Accept-Charset"]=>
9// string(31) "ISSO-8859-1[...]"
10// ["Accept-Encoding"]=>
11// string(17) "gzip,deflate,sdch"
12// ["Accept-Language"]=>
13// string(14) "en-US,en;q=0.8"
14// ["Cache-Control"]=>
15// string(9) "max-age=0"
16// ["Connection"]=>
17// string(10) "keep-alive"
18// ["Host"]=>
19// string(9) "localhost"
20// ["User-Agent"]=>
21// string(108) "Mozilla/5.0 (Windows NT 6.1; WOW64) [...]"
22// }
1// set post fields
2$post = [
3 'username' => 'user1',
4 'password' => 'passuser1',
5 'gender' => 1,
6];
7
8$ch = curl_init('http://www.example.com');
9curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
10curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
11
12// execute!
13$response = curl_exec($ch);
14
15// close the connection, release resources used
16curl_close($ch);
17
18// do anything you want with your response
19var_dump($response);
1<?php
2
3$post = [
4 'username' => 'user1',
5 'password' => 'passuser1',
6 'gender' => 1,
7];
8$ch = curl_init();
9curl_setopt($ch, CURLOPT_URL, 'http://www.domain.com');
10curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
11curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
12$response = curl_exec($ch);
13var_export($response);
14
1PHP cURL GET Request
2A GET request retrieves data from a server. This can be a website’s HTML, an API response or other resources.
3
4<?php
5
6$cURLConnection = curl_init();
7
8curl_setopt($cURLConnection, CURLOPT_URL, 'https://hostname.tld/phone-list');
9curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);
10
11$phoneList = curl_exec($cURLConnection);
12curl_close($cURLConnection);
13
14$jsonArrayResponse - json_decode($phoneList);
1$headers = array(
2 'Content-Type: application/json',
3 "x-access-token: $token"
4 );
5 $urlPost = "/child/all";
6 $url = "ip/apiurl"
7 $curl = curl_init($url);
8 curl_setopt($curl, CURLOPT_URL, $url);
9 curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
10 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
11 $response = curl_exec($curl);
12 curl_close($curl);
13 $jsonObject = json_decode($response);
14 return $jsonObject;