Skip to content

More examples for CURL #4612

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 68 additions & 7 deletions reference/curl/examples.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,80 @@
<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>
<title>Basic curl examples</title>
<para>
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
<function>curl_exec</function> and then you finish off
your session using the <function>curl_close</function>.
Here is an example that uses the cURL functions to fetch the
and then send the request with the <function>curl_exec</function>.
</para>
<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>
Expand All @@ -32,9 +95,7 @@ curl_exec($ch);
if(curl_error($ch)) {
fwrite($fp, curl_error($ch));
}
curl_close($ch);
fclose($fp);
?>
]]>
</programlisting>
</example>
Expand Down