PHP code example of squareboat / laravel-simplecurl
1. Go to this page and download the library: Download squareboat/laravel-simplecurl library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?phprequire_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
functiongetUser($id){
/*
* Please ensure only a single Model is present in the response for this. Multiple rows will not be
* automatically get converted into Collections And Models atm.
*
* Keys set as fillable in that particular model are used here. Any fillable key, not present in the
* response will be set as null and an instance of the Model will be returned.
*/
$userModel = SimpleCurl::get('http://mysite.com/api/v1/user/' .id. '/get/')->getResponseAsModel('App\User')
/*
* There is also a second parameter which you can use to add something from the response as a relation
* to it.
*
* You will have to save a copy of the model somewhere so that SimpleCurl can get apiAttributes/fillable fields from
* that class and use for relational Models as well.
*/
$relations = [
[
'photo' => 'App\Photo'
],
[
'city'=> 'App\City', //This will work as city.state and give state as a relation to city'state' => 'App\State'
]
];
$userModelWithPhotoAsRelation = SimpleCurl::get('http://mysite.com/api/v1/user/' .id. '/get/')->getResponseAsModel('App\User', $relations);
}
useSimpleCurl;
classUsersApiRepo{
/*
* A Config Variable which you can use to handle multiple CURL requests...
*/protected $simpleCurlConfig;
function__construct(){
$this->simpleCurlConfig = [
'connectTimeout' => 30,
'dataTimeout' => 60,
'baseUrl' => 'http://mysite.com/',
'defaultHeaders' => [
'Authorization: Bearer {bearer_token}',
'Content-Type: application/json'
],
];
}
functionallUsers(){
// Set Defaults for making a CURL Request
$simpleCurl = SimpleCurl::setConfig($this->simpleCurlConfig);
// or if you just want to set base url// $simpleCurl = SimpleCurl::setBaseUrl($this->simpleCurlConfig['baseUrl']);// Gives Response As Array
$usersArray = $simpleCurl->get('api/v1/users/all')->getResponseAsArray();
and so on...
}
and so on.....
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.