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// 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);
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$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;
1$data = array(
2 "userId" => "1"
3 );
4
5 $headers = array(
6 'Content-Type: application/json'
7 );
8 $url = "ip/api";
9 $curl = curl_init($url);
10 curl_setopt($curl, CURLOPT_POST, 1);
11 curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
12 curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
13 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
14 $response = curl_exec($curl);
15 curl_close($curl);
16 $jsonObject = json_decode($response);
17 return $jsonObject;
1$ch = curl_init();
2$curlConfig = array(
3 CURLOPT_URL => "http://www.example.com/yourscript.php",
4 CURLOPT_POST => true,
5 CURLOPT_RETURNTRANSFER => true,
6 CURLOPT_POSTFIELDS => array(
7 'field1' => 'some date',
8 'field2' => 'some other data',
9 )
10);
11curl_setopt_array($ch, $curlConfig);
12$result = curl_exec($ch);
13curl_close($ch);
14
15// result sent by the remote server is in $result
16