Search
 
SCRIPT & CODE EXAMPLE
 

PHP

curl in php

$url="http://abc/api/xyz.php";  //url of 2nd website where data is to be send
$postdata = $data
$ch = curl_init(); 
curl_setopt ($ch, CURLOPT_URL, $url); 
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0)
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt ($ch, CURLOPT_POST, 1); 
curl_setopt ($ch, CURLOPT_TIMEOUT, 60); 
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_HTTPHEADER, array('Content-Type', 'application/json'));
$result = curl_exec ($ch); 

echo $result;  
curl_close($ch);
Comment

php curl example

$ch = curl_init();
$curlConfig = array(
    CURLOPT_URL            => "http://www.example.com/yourscript.php",
    CURLOPT_POST           => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POSTFIELDS     => array(
        'field1' => 'some date',
        'field2' => 'some other data',
    )
);
curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
curl_close($ch);

// result sent by the remote server is in $result
Comment

simple php curl

<?php
 
// From URL to get webpage contents
$url = "https://www.geeksforgeeks.org/";
 
// Initialize a CURL session.
$ch = curl_init();
 
// Return Page contents.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 
// Grab URL and pass it to the variable
curl_setopt($ch, CURLOPT_URL, $url);
 
$result = curl_exec($ch);
 
echo $result;
 
?>
Comment

php use curl


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.
Comment

PREVIOUS NEXT
Code Example
Php :: php undefined array key 
Php :: jobs laravel 
Php :: PHP readfile() Function 
Php :: how to auto calculate price in mysql table and php 
Php :: [name] 
Php :: php inline variables string 
Php :: check if order id exists wordpress woccommerce 
Php :: laravel gigapay get single employee 
Php :: comparison operators in php 
Php :: laravel first or with callback 
Php :: php get list of months by year 
Php :: getting key from env returns null laravel 
Php :: Generating Random String In PHP Using Brute Force 
Php :: adding n days to a DATETIME format value 
Php :: cake php 2.x group 
Php :: laravel mix build as umd 
Php :: $_FILES image dimensions 
Php :: Dropzone Attachment Required is not working 
Php :: upload image to backend (see rest of the link) 
Php :: TypeError IlluminateAuthSessionGuard::login(): Argument #1 ($user) must be of type IlluminateContractsAuthAuthenticatable 
Php :: laravel gigapay payout list 
Php :: auto check a category when creating new post 
Php :: yii2 has many where 
Php :: checking why sql query not connecting to database 
Php :: post with count greater than 1 laravel 
Php :: query builder laravel getmedia undefined method 
Php :: wordpress give query a unique identity 
Php :: leggere file su piu righe php 
Php :: Reference — What does this symbol mean in PHP? 
Php :: 500 Internal Server Error mamp rest api PDO 
ADD CONTENT
Topic
Content
Source link
Name
7+6 =