Search
 
SCRIPT & CODE EXAMPLE
 

PHP

CURL PHP POST

<?php

$post = [
    'username' => 'user1',
    'password' => 'passuser1',
    'gender'   => 1,
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.domain.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
$response = curl_exec($ch);
var_export($response);
Comment

php curl

// set post fields
$post = [
    'username' => 'user1',
    'password' => 'passuser1',
    'gender'   => 1,
];

$ch = curl_init('http://www.example.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

// execute!
$response = curl_exec($ch);

// close the connection, release resources used
curl_close($ch);

// do anything you want with your response
var_dump($response);
Comment

php curl

function curl( string $url, array $data)
{
	function send($url, $request)
	{
		$ch = curl_init();
		$headers = [
			'Accept: application/json',
			'Content-Type: application/json',
		];
		$options = [
			CURLOPT_URL => $url,
			CURLOPT_POST => true,
			CURLOPT_POSTFIELDS => $request,
			CURLOPT_FOLLOWLOCATION => true,
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_HEADER => true,
			CURLOPT_HTTPHEADER => $headers,
		];

		curl_setopt_array($ch, $options);
		$response = curl_exec($ch);
		curl_close($ch);
		
		return $response;
	}

	return send($url, json_encode($data));
}
Comment

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

curl php

// Initialize Curl 
 $curl = curl_init();
 curl_setopt($curl, CURLOPT_URL, "https://coinmarketcap.com/"); // set live website where data from
 curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); // default
 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // default
 $content = curl_exec($curl);

 preg_match_all('!<p color="text3" class="sc-AxhUy bzeXdk coin-item-symbol" font-size="1">(.*?)</p>!', $content, $matches);

 var_dump($matches);
Comment

php curl

<?php
// create a new cURL resource
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);

// grab URL and pass it to the browser
curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);
?>
Comment

PHP cURL request

function &web_curl_http($url) 
{  
   $c = curl_init();
   curl_setopt( $c , CURLOPT_URL , $url);
   curl_setopt( $c , CURLOPT_USERAGENT, "Mozilla/5.0 (Linux Centos 7;) Chrome/74.0.3729.169 Safari/537.36");
   curl_setopt( $c , CURLOPT_RETURNTRANSFER, true);
   curl_setopt( $c , CURLOPT_SSL_VERIFYPEER, false);
   curl_setopt( $c , CURLOPT_SSL_VERIFYHOST, false);
   curl_setopt( $c , CURLOPT_TIMEOUT, 10000); // 10 sec
   $data = curl_exec($c);
   curl_close($c);
   return $data;
}
Comment

PHP-Curl

$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
$result = curl_exec($ch);
curl_close($ch);
//check the result
var_dump($result);
Comment

php curl request

$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);
Comment

Php CURL

$ch = curl_init('http://example.com/image.php');
$fp = fopen('/my/folder/flower.gif', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
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

php curl

$data = json_encode(array(
    "ShortCode" => "600981",
    "ResponseType" => "Completed",
    "ConfirmationURL" => "https://mydomainx.com/confirmation",
    "ValidationURL" => "https://mydomainx.com/validation",
));

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
Comment

PREVIOUS NEXT
Code Example
Php :: laravel use model inside blade 
Php :: php get domain name with https 
Php :: generate token in php 
Php :: copy env example to .env in laravel 
Php :: php artisan services 
Php :: php trim array to certain length 
Php :: validate executable path vscode 
Php :: readline php 
Php :: access storage from the view laravel 6 
Php :: php current date get 
Php :: convert comma separated number to number in php 
Php :: bind in pdo 
Php :: remove repeated columns laravel 
Php :: operador in laravel 
Php :: specified key was too long max key length is 767 bytes 
Php :: form submitting twice 
Php :: laravel select all except some columns 
Php :: Convert Carbon Seconds Into Days Hours Minute 
Php :: How to get only year, month and day from timestamp in Laravel 
Php :: woocommerce get user id by email 
Php :: Using middleware auth laravel in controller constructor 
Php :: auth guard api is not defined laravel 8 
Php :: laravel query get big table records 
Php :: time duration calculation laravel 
Php :: running laravel project in mobile phone 
Php :: get last month php 
Php :: check if url is https laravel 
Php :: wordpress remove user roles 
Php :: laravel faker title 
Php :: php memory usage view 
ADD CONTENT
Topic
Content
Source link
Name
4+4 =