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}
1// Get cURL resource
2$curl = curl_init();
3// Set some options - we are passing in a useragent too here
4curl_setopt_array($curl, [
5 CURLOPT_RETURNTRANSFER => 1,
6 CURLOPT_URL => 'http://testcURL.com',
7 CURLOPT_USERAGENT => 'Codular Sample cURL Request',
8 CURLOPT_POST => 1,
9 CURLOPT_POSTFIELDS => [
10 item1 => 'value',
11 item2 => 'value2'
12 ]
13]);
14// Send the request & save response to $resp
15$resp = curl_exec($curl);
16// Close request to clear up some resources
17curl_close($curl);
18
1// The ultimate function for all php curl requests all in one
2function curl( $api_url,$request = 'get' , $params = array() , $mode = false , $timeout='')
3{
4 $request = strtolower($request);
5
6 $ch = curl_init($api_url);
7
8 if($request == 'post')
9 {
10 curl_setopt ($ch, CURLOPT_POST, TRUE);
11 curl_setopt ($ch, CURLOPT_POSTFIELDS, http_build_query($params));
12 }
13
14 if( $timeout != '' )
15 {
16 curl_setopt ($ch, CURLOPT_TIMEOUT, $timeout);
17 }
18
19 if($request == 'put')
20 {
21 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
22 curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
23 }
24
25 if($request == 'delete')
26 {
27 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
28 }
29
30 curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, false);
31 curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, false);
32 curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
33
34 $response = curl_exec($ch);
35
36 if($mode)
37 $response = json_decode($response , $mode);
38 else
39 $response = json_decode($response);
40
41 return $response;
42}