PHP Curl Get Request with Parameters Example

PHP Curl Get Request with Parameters

Example: index.php

<?php
  
    $my_ch = curl_init();
  
    $end_point_name = "https://api.mywebtuts.com/api/users";
    $all_data = ['page' => 5];
  
    $data = http_build_query($all_data);
  
    $myLiveLink = $end_point_name."?".$data;
  
    curl_setopt($my_ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($my_ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($my_ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($my_ch, CURLOPT_URL, $myLiveLink);
    curl_setopt($my_ch, CURLOPT_TIMEOUT, 80);
       
    $response = curl_exec($my_ch);
        
    if(curl_error($my_ch)){
        echo 'Request Error:' . curl_error($my_ch);
    }else{
        echo $response;
    }
       
    curl_close($my_ch);
  
?>

Result:

{"current_page":1,"data":[{"id":1,"first_name":"Johnny","last_name":"Mark","email":"[email protected]","avatar":"https://api.mywebtuts.com/asset/img/photo-1.png"},{"id":2,"first_name":"Varli","last_name":"Khan","email":"[email protected]","avatar":"https://api.mywebtuts.com/asset/img/photo-2.png"},{"id":3,"first_name":"Lawson","last_name":"Ferguson","email":"[email protected]","avatar":"https://api.mywebtuts.com/asset/img/photo-8.png"},{"id":4,"first_name":"Tobias","last_name":"Michael","email":"[email protected]","avatar":"https://api.mywebtuts.com/asset/img/photo-4.png"},{"id":5,"first_name":"George","last_name":"Fields","email":"[email protected]","avatar":"https://api.mywebtuts.com/asset/img/photo-5.png"},{"id":6,"first_name":"Byron","last_name":"Edwards","email":"[email protected]","avatar":"https://api.mywebtuts.com/asset/img/photo-6.png"}],"first_page_url":"https://api.mywebtuts.com/api/users?page=1","from":1,"last_page":2,"last_page_url":"https://api.mywebtuts.com/api/users?page=2","links":[{"url":null,"label":"« Previous","active":false},{"url":"https://api.mywebtuts.com/api/users?page=1","label":"1","active":true},{"url":"https://api.mywebtuts.com/api/users?page=2","label":"2","active":false},{"url":"https://api.mywebtuts.com/api/users?page=2","label":"Next »","active":false}],"next_page_url":"https://api.mywebtuts.com/api/users?page=2","path":"https://api.mywebtuts.com/api/users","per_page":6,"prev_page_url":null,"to":6,"total":12}

See also :  require and require_once in php - PHP require and require_once

Leave a Reply

Your email address will not be published. Required fields are marked *