Operations related to curl library in php

Recently, the curl library of php is used in the development to transmit and receive http messages. Because I am not good at php, I have taken a lot of detours. I will summarize it here. If there are any mistakes, please correct me

1. The structure of the HTTP session

Method URL Version

Hearder

Data

2. The meaning of the relevant functions used

$ch = curl_init(); //Initialize a curl session
curl_setopt(); //Set curl transfer options
curl_setopt($ch, CURLOPT_URL, $url); //Set the fetched url
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1) ; //Set the obtained information to be returned in the form of a file stream, rather than output directly


The following 6 lines are a set of
$cacert = "/path/to/client.crt";
$key = "/path/to/client.key";
curl_setopt($ch, CURLOPT_SSLCERT, $cacert); //Specify the client certificate , for https
curl_setopt($ch, CURLOPT_SSLKEY, $key); //Specify the client secret key
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); //Combined with the next sentence, bypass ssl verification
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

curl_setopt($ch, CURLOPT_HTTPGET, true); //Set the transmission mode to get, in fact, the default is also get, you can set
curl_setopt($ch, CURLOPT_POST, 1); //Set the transmission mode to post
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query($data)); //Use the post method to transmit the message with the Body content of data, get is similar to
$output = curl_exec($ch); //Execute the curl session and get the returned result
curl_errno($ch ); //returns a string containing the error message of the current session
curl_close($ch); //closes the curl request
print_r($output); //prints the acquired data

3. Encapsulate Header data and Data data and transmit

(1) Header封装
$accessToken = "+++++++";
$app_key = "+++++++";
$header = array("Content-Type: application/json","Authorization: bearer $accessToken","app_key: $app_key");
curl_setopt($ch,CURLOPT_HTTPHEADER,$header);   //为url添加hearder

(2) Data封装


If Hearder's Content-type: application/x-www-form-urlencoded, then Data is encapsulated as follows
$data = Array ("deviceId" => "$deviceId", "gatewayId" => "$deviceId");
$data = http_build_query($data); //Data needs to be processed by this function
curl_setopt($ch, CURLOPT_URL, $url.$data); //http uses the get method to transfer the url with data
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); / /http uses the post method to transmit the url with data


If the Content-type of Hearder: application/json, then Data is encapsulated as follows
$data = '{"notifyType":"deviceAdded","callbackurl":" https://192.168.1.1:443/test/a.php "} ' ; //Data does not need function processing
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //http uses post method to transmit the url of json format data

4.php receives post messages in different formats

(1) If the received message is Content-type: application/x-www-form-urlencoded type
$post_data = $_POST; //$post_data is an array, you can directly take the value of the element, such as $post_data['id']

(2) If the received message is Content-type: application/json type
$raw_post_data = file_get_contents("php://input"); //&raw_post_data is in json format, you need to use the json_decode function to process
$raw_post_data = json_decode($raw_post_data, true); //$raw_post_data is an array after processing, you can directly take the value of the element

Note
: If you want to write the received data into a txt file, you need to convert it into json format data, then $post_data in 1 needs to use json_encode Processing, $raw_post_data in 2 does not need to be processed with json_decode
$post_data=json_encode($post_data); //Convert to json format
file = fopen("test.txt","w"); //Open txt file
$fwrite( $file,$post_data); //Write data to file

5. Attach the code

(1) Post method:


<?php
$ch = curl_init();
$cacert = "/path/to/client.crt";
$key = "/path/to/client.key";
$url = "https://192.168.1.1:443/login";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSLCERT, $cacert);
curl_setopt($ch, CURLOPT_SSLKEY, $key);
curl_setopt($ch, CURLOPT_POST, 1);
$post_data = Array
(
"appId" => "++++++++++++++++",
"secret" => "++++++++++++++++"
);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
$output = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Errno'.curl_error($ch);
}
curl_close($ch);
print_r($output);
?>


(2) get method:

<?php
$ch = curl_init();
$cacert = "/path/to/client.crt";
$key = "/path/to/client.key";
$accessToken = "+++++++++++++++++++";
$deviceId = "+++++++++++++++++++";
&app_key = "+++++++++++++++++";
$url = "https://192.168.1.1:443/deviceData";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSLCERT, $cacert);
curl_setopt($ch, CURLOPT_SSLKEY, $key);
$header = array("Content-Type: application/json","Authorization: bearer $accessToken","app_key: &app_key");
curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
$data = Array
(
"deviceId" => "$deviceId",
"gatewayId" => "$deviceId"
);
$data = http_build_query($data);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_URL, $url.$data);
$output = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Errno'.curl_error($ch);
}
curl_close($ch);
print_r($output);
?>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325687996&siteId=291194637