PHP 5.6.18 is available

Exemple avec curl

Une fois PHP compilé avec le support cURL, vous pouvez commencer à utiliser les fonctions cURL. La première des choses à faire est d'initialiser une session cURL avec la fonction curl_init(), puis, vous pouvez définir toutes vos options pour le transfert avec la fonction curl_setopt(), et enfin, vous pouvez exécuter la session avec curl_exec() et y mettre fin avec la fonction curl_close(). Voici un exemple qui utilise les fonctions cURL pour récupérer la page d'accueil du site example.com dans un fichier :

Exemple #1 Utilisation du module cURL pour récupérer la page d'accueil de example.com

<?php

$ch 
curl_init("http://www.example.com/");
$fp fopen("example_homepage.txt""w");

curl_setopt($chCURLOPT_FILE$fp);
curl_setopt($chCURLOPT_HEADER0);

curl_exec($ch);
curl_close($ch);
fclose($fp);
?>

add a note add a note

User Contributed Notes 4 notes

up
51
Anon
1 year ago
IMO this example would have been better if it had done a check for curl_error(), in order to advertize the existence of this function to people learning about cURL who try the example but mysteriously get no response back for whatever reason.
up
18
Roberto Braga
10 months ago
It is important to notice that when using curl to post form data and you use an array for CURLOPT_POSTFIELDS option, the post will be in multipart format

<?php
$params
=['name'=>'John', 'surname'=>'Doe', 'age'=>36)
$defaults = array(
CURLOPT_URL => 'http://myremoteservice/',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $params,
);
$ch = curl_init();
curl_setopt_array($ch, ($options + $defaults));
?>
This produce the following post header:

--------------------------fd1c4191862e3566
Content-Disposition: form-data; name="name"

Jhon
--------------------------fd1c4191862e3566
Content-Disposition: form-data; name="surnname"

Doe
--------------------------fd1c4191862e3566
Content-Disposition: form-data; name="age"

36
--------------------------fd1c4191862e3566--

Setting CURLOPT_POSTFIELDS as follow produce a standard post header

CURLOPT_POSTFIELDS => http_build_query($params),

Which is:
name=John&surname=Doe&age=36

This caused me 2 days of debug while interacting with a java service which was sensible to this difference, while the equivalent one in php got both format without problem.
up
-30
simon dot riget at gmail dot com
2 years ago
Using ArangoDB with PHP

Coding with a NoSQL data base like ArangoDB can be made more complicated then need be. A simple solution to code for ArangoDB is to use the REST interface directly.
All you need is a few initialization variables and a small function to ease the typing - and of cause install the php5-curls module...
See ArangoDB REST API documentation for options and request types.

<?php
// Initialize options for REST interface
$adb_url="http://127.0.0.1:8529";
$adb_option_defaults = array(
   
CURLOPT_HEADER => false,
   
CURLOPT_RETURNTRANSFER => true,
   
CURLOPT_TIMEOUT => 2
 
);

// ArangoDB REST function.
// Connection are created demand and closed by PHP on exit.
function adb_rest($method,$uri,$querry=NULL,$json=NULL,$options=NULL){
  global
$adb_url,$adb_handle,$adb_option_defaults;

 
// Connect
 
if(!isset($adb_handle)) $adb_handle = curl_init();

  echo
"DB operation: $method $uri $querry $json\n";

 
// Compose querry
 
$options = array(
   
CURLOPT_URL => $adb_url.$uri."?".$querry,
   
CURLOPT_CUSTOMREQUEST => $method, // GET POST PUT PATCH DELETE HEAD OPTIONS
   
CURLOPT_POSTFIELDS => $json,
  );
 
curl_setopt_array($adb_handle,($options + $adb_option_defaults));

 
// send request and wait for responce
 
$responce json_decode(curl_exec($adb_handle),true);

  echo
"Responce from DB: \n";
 
print_r($responce);
 
  return(
$responce);
}
?>

Here's some examples:

<?php
// Create a collection
$responce = adb_rest("POST","/_api/collection","",'{"name" : "test"}');

// Create a document
$responce = adb_rest("POST","/_api/document","collection=test",'{ "hello" : "World" }');

// List documents in a collection
$responce=adb_rest("GET","/_api/document","collection=test");

// Change document
$document_handle=$responce['documents'][0];
$responce=adb_rest("PATCH",$document_handle,"",'{ "hello" : "World of mine" }');

// Show document
$responce=adb_rest("GET",$document_handle);

// Search
$responce=adb_rest("POST","/_api/cursor","",'{
  "query" : "FOR t IN test RETURN t",
  "count" : true,
  "batchSize" : 50
}'
);

while(
$responce['hasMore'])
 
$responce=adb_rest("PUT","/_api/cursor/".$responce['id']);

// Delete document
$responce=adb_rest("DELETE",$document_handle);

// DB Delete collection
$responce=adb_rest("DELETE","/_api/collection/test");

// to handle errors, add something like this:
if($responce['error']) die ("can't create collection");

?>
up
-48
Anon
1 year ago
The example fetches a resource (the homepage of a website) and writes it to the the file represented by $fp.

Judging from comments elsewhere in this chapter, the example given ought to set CURLOPT_RETURNTRANSFER to true prior to setting CURLOPT_FILE.
To Top