Answers for "curl get request php example"

PHP
10

http post request php curl

$post = [
   'teste' => $_POST['teste']
];
httpPost('url.com', $post);
// function
function httpPost($url, $data)
{
   	$curl = curl_init($url);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($curl);
    curl_close($curl);
    return $response;
}
Posted by: Guest on June-08-2020
3

php curl get

PHP cURL GET Request
A GET request retrieves data from a server. This can be a website’s HTML, an API response or other resources.

<?php

$cURLConnection = curl_init();

curl_setopt($cURLConnection, CURLOPT_URL, 'https://hostname.tld/phone-list');
curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);

$phoneList = curl_exec($cURLConnection);
curl_close($cURLConnection);

$jsonArrayResponse - json_decode($phoneList);
Posted by: Guest on April-04-2020
8

php curl post

// 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);
Posted by: Guest on May-15-2020
2

php curl

// The ultimate function for all php curl requests all in one
function curl( $api_url,$request = 'get' , $params = array() , $mode = false , $timeout='')
{
    $request = strtolower($request);

    $ch = curl_init($api_url);

    if($request == 'post')
    {
        curl_setopt ($ch, CURLOPT_POST, TRUE);
        curl_setopt ($ch, CURLOPT_POSTFIELDS, http_build_query($params));
    }
    
    if( $timeout != ''  )
    {
        curl_setopt ($ch, CURLOPT_TIMEOUT, $timeout);
    }

    if($request == 'put')
    {
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
    }
  
    if($request == 'delete')
    {
      curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
    }

    curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);

    $response     = curl_exec($ch);

    if($mode)
        $response     = json_decode($response , $mode);
    else
        $response     = json_decode($response);    

    return $response;
}
Posted by: Guest on July-03-2021
1

curl php example

function makeAPICall($url){

        
        $handle = curl_init();

         
        // Set the url
        curl_setopt($handle, CURLOPT_URL, $url);
        // Set the result output to be a string.
        curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
         
        $output = curl_exec($handle);
         
        curl_close($handle);
         
        echo $output;
    return $output;
    }
Posted by: Guest on July-18-2020
-1

API call in PHP using cURL

// Method: POST, PUT, GET etc
// Data: array("param" => "value") ==> index.php?param=value

function CallAPI($method, $url, $data = false)
{
    $curl = curl_init();

    switch ($method)
    {
        case "POST":
            curl_setopt($curl, CURLOPT_POST, 1);

            if ($data)
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            break;
        case "PUT":
            curl_setopt($curl, CURLOPT_PUT, 1);
            break;
        default:
            if ($data)
                $url = sprintf("%s?%s", $url, http_build_query($data));
    }

    // Optional Authentication:
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($curl, CURLOPT_USERPWD, "username:password");

    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

    $result = curl_exec($curl);

    curl_close($curl);

    return $result;
}
Posted by: Guest on June-14-2021

Browse Popular Code Answers by Language