[doc-en] master: More examples for CURL (#4612)

From: Date: Fri, 21 Aug 2026 14:46:51 +0000
Subject: [doc-en] master: More examples for CURL (#4612)
Groups: php.doc.cvs 
Request: Send a blank email to doc-cvs+get-23608@lists.php.net to get a copy of this message
Author: colshrapnel (colshrapnel) Committer: GitHub (web-flow) Pusher: lacatoire Date: 2026-08-21T16:46:48+02:00 Commit: https://github.com/php/doc-en/commit/e7c6a2f6847c3a8c9fec6ba588f235ccdcf6f3a5 Raw diff: https://github.com/php/doc-en/commit/e7c6a2f6847c3a8c9fec6ba588f235ccdcf6f3a5.diff More examples for CURL (#4612) * More examples for CURL Added examples for the most used requests * Update examples.xml * Update examples.xml Changed paths: M reference/curl/examples.xml Diff: diff --git a/reference/curl/examples.xml b/reference/curl/examples.xml index e657ee9eb71d..d901a7856f95 100644 --- a/reference/curl/examples.xml +++ b/reference/curl/examples.xml @@ -4,16 +4,81 @@ <chapter xml:id="curl.examples" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink"> &reftitle.examples; <section xml:id="curl.examples-basic"> - <title>Basic curl example</title> - <para> + <title>Basic curl examples</title> + <simpara> 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 <function>curl_init</function>, then you can set all your options for the transfer via the <function>curl_setopt</function>, - then you can execute the session with the + then you can execute the session with the <function>curl_exec</function>. - Here is an example that uses the cURL functions to fetch the + </simpara> + <para> + Here is a simple example that uses the cURL functions to send + a POST request: + <example> + <title>Using PHP's cURL module to send a POST request</title> + <programlisting role="php"> +<![CDATA[ +<?php +$data = ['foo' => '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)); +} +]]> + </programlisting> + </example> + </para> + <para> + Another example that uses the cURL functions to send a raw JSON + POST request: + <example> + <title>Using PHP's cURL module to send a JSON POST request</title> + <programlisting role="php"> +<![CDATA[ +<?php +$post_data = ['foo' => '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)); +} +]]> + </programlisting> + </example> + </para> + <para> + Here is another example that uses the cURL functions to fetch the example.com homepage into a file: <example> <title>Using PHP's cURL module to fetch the example.com homepage</title> @@ -32,7 +97,6 @@ if(curl_error($ch)) { fwrite($fp, curl_error($ch)); } fclose($fp); -?> ]]> </programlisting> </example>

« previous php.doc.cvs (#23608) next »