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);
1function getUrl($url){
2 $ch = curl_init($url);
3 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
4 $response = curl_exec($ch);
5 curl_close($ch);
6 return $response;
7}
1function makeAPICall($url){
2
3
4 $handle = curl_init();
5
6
7 // Set the url
8 curl_setopt($handle, CURLOPT_URL, $url);
9 // Set the result output to be a string.
10 curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
11
12 $output = curl_exec($handle);
13
14 curl_close($handle);
15
16 echo $output;
17 return $output;
18 }
1function callAPI($method, $url, $data){
2 $curl = curl_init();
3 switch ($method){
4 case "POST":
5 curl_setopt($curl, CURLOPT_POST, 1);
6 if ($data)
7 curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
8 break;
9 case "PUT":
10 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
11 if ($data)
12 curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
13 break;
14 default:
15 if ($data)
16 $url = sprintf("%s?%s", $url, http_build_query($data));
17 }
18 // OPTIONS:
19 curl_setopt($curl, CURLOPT_URL, $url);
20 curl_setopt($curl, CURLOPT_HTTPHEADER, array(
21 'APIKEY: 111111111111111111111',
22 'Content-Type: application/json',
23 ));
24 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
25 curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
26 // EXECUTE:
27 $result = curl_exec($curl);
28 if(!$result){die("Connection Failure");}
29 curl_close($curl);
30 return $result;
31}