From 3946f232017b0c6a27e356ecc0ebdf640e25fb32 Mon Sep 17 00:00:00 2001
From: colshrapnel
Date: Sat, 12 Apr 2025 11:30:29 +0300
Subject: [PATCH 1/3] More examples for CURL
Added examples for the most used requests
---
reference/curl/examples.xml | 74 ++++++++++++++++++++++++++++++++++---
1 file changed, 68 insertions(+), 6 deletions(-)
diff --git a/reference/curl/examples.xml b/reference/curl/examples.xml
index 71d6f7847110..c456a9de6234 100644
--- a/reference/curl/examples.xml
+++ b/reference/curl/examples.xml
@@ -4,17 +4,80 @@
&reftitle.examples;
- Basic curl example
+ Basic curl examples
Once you've compiled PHP with cURL support, you can begin using
the cURL functions. The basic idea behind the cURL functions is
that you initialize a cURL session using the
curl_init, then you can set all your
options for the transfer via the curl_setopt,
- then you can execute the session with the
- curl_exec and then you finish off
- your session using the curl_close.
- Here is an example that uses the cURL functions to fetch the
+ and then send the request with the curl_exec.
+
+
+ Here is a simple example that uses the cURL functions to send
+ a POST request:
+
+ Using PHP's cURL module to send a POST request
+
+ 'bar', 'baz' => 48];
+$url = "http://www.example.com/handler.php";
+
+$ch = curl_init($url);
+
+// tell CURL to return the response instead of sending it to stdout
+curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+
+// set the POST data, corresponding method and headers:
+curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
+
+// send the request and get the response
+$response = curl_exec($ch);
+
+if(curl_error($ch)) {
+ // handle the error, or just
+ throw new RuntimeException(curl_error($ch));
+}
+]]>
+
+
+
+
+ Another example that uses the cURL functions to send a raw JSON
+ POST request:
+
+ Using PHP's cURL module to send a JSON POST request
+
+ 'bar', 'baz' => 48];
+$url = "http://www.example.com/api/endpoint";
+
+$ch = curl_init($url);
+
+// tell CURL to return the response instead of sending it to stdout
+curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+
+// set the POST data and corresponding method
+curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data));
+
+// set the required header
+curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
+
+// send the request and get the response
+$response = curl_exec($ch);
+
+if(curl_error($ch)) {
+ // handle the error, or just
+ throw new RuntimeException(curl_error($ch));
+}
+]]>
+
+
+
+
+ Here is another example that uses the cURL functions to fetch the
example.com homepage into a file:
Using PHP's cURL module to fetch the example.com homepage
@@ -34,7 +97,6 @@ if(curl_error($ch)) {
}
curl_close($ch);
fclose($fp);
-?>
]]>
From 24bb8b415bf56032db279889128edd23cc0d3e8d Mon Sep 17 00:00:00 2001
From: colshrapnel
Date: Sat, 12 Apr 2025 11:40:14 +0300
Subject: [PATCH 2/3] Update examples.xml
---
reference/curl/examples.xml | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)
diff --git a/reference/curl/examples.xml b/reference/curl/examples.xml
index c456a9de6234..d7e510fc7e66 100644
--- a/reference/curl/examples.xml
+++ b/reference/curl/examples.xml
@@ -11,7 +11,7 @@
that you initialize a cURL session using the
curl_init, then you can set all your
options for the transfer via the curl_setopt,
- and then send the request with the curl_exec.
+ and then send the request with the curl_exec.
Here is a simple example that uses the cURL functions to send
@@ -53,7 +53,7 @@ if(curl_error($ch)) {
'bar', 'baz' => 48];
$url = "http://www.example.com/api/endpoint";
-
+
$ch = curl_init($url);
// tell CURL to return the response instead of sending it to stdout
@@ -95,7 +95,6 @@ curl_exec($ch);
if(curl_error($ch)) {
fwrite($fp, curl_error($ch));
}
-curl_close($ch);
fclose($fp);
]]>
From 63e6beff19abc58f01736608d52cc013ddb4aa64 Mon Sep 17 00:00:00 2001
From: colshrapnel
Date: Sat, 12 Apr 2025 11:44:17 +0300
Subject: [PATCH 3/3] Update examples.xml
---
reference/curl/examples.xml | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/reference/curl/examples.xml b/reference/curl/examples.xml
index d7e510fc7e66..538211210454 100644
--- a/reference/curl/examples.xml
+++ b/reference/curl/examples.xml
@@ -29,7 +29,7 @@ $ch = curl_init($url);
// tell CURL to return the response instead of sending it to stdout
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
-// set the POST data, corresponding method and headers:
+// set the POST data, corresponding method and headers
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
// send the request and get the response
@@ -77,7 +77,7 @@ if(curl_error($ch)) {
- Here is another example that uses the cURL functions to fetch the
+ Here is another example that uses the cURL functions to fetch the
example.com homepage into a file:
Using PHP's cURL module to fetch the example.com homepage