Search
 
SCRIPT & CODE EXAMPLE
 

PHP

php curl post json

$ch = curl_init( $url );
# Setup request to send json via POST.
$payload = json_encode( array( "customer"=> $data ) );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
# Return response instead of printing.
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
# Send request.
$result = curl_exec($ch);
curl_close($ch);
Comment

php post curl json

function postCurl(array $postFields, string $url)
{
    $post = $postFields;
    if (count($post) == 0) {
        return false;
    }
    $url = trim($url);
    $postdata = json_encode($post, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_POST, 1);
    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);
    $decodedResult = json_decode($result, JSON_UNESCAPED_UNICODE);
    if ($decodedResult['status'] == 'ok') {
        return $decodedResult;
    } else {
        return false;
    }
    curl_close($ch);
}
Comment

php curl post json

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

PREVIOUS NEXT
Code Example
Php :: check if a string is url or not php 
Php :: date add one day php 
Php :: php object to xml 
Php :: firebase jwt php verify 
Php :: difference between two timestamps php 
Php :: fecade Artisan:call laravel 
Php :: Morocco 
Php :: php length of array 
Php :: loop through months and year php 
Php :: is_array php 
Php :: php get intersection of arrays 
Php :: php pass a variabele to js 
Php :: foreach skip first php 
Php :: php pdo set charset 
Php :: laravel orwhere 
Php :: how check if method is not in class in php 
Php :: php mysql error 
Php :: symfony convert entity to array 
Php :: centos excecutable php 
Php :: Laravel 8 query builder, Inner Join Clause 
Php :: laravel blade @guest 
Php :: 500 error php 
Php :: laravel where has 
Php :: get cart item by cart item key woocommerce 
Php :: php from 
Php :: drupal 8 get field entities 
Php :: laravel decrement 
Php :: how to get value of textarea in php 
Php :: get first element of array php 
Php :: Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, bool given in 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =