Call a REST API in PHP
You can access any REST API with PHPs cURL
Extension. However, the API Documentation (Methods, Parameters etc.) must be provided by your Client!
Example:
// 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;
}
Calling the rest api in php
Here is the php code to send request using curl.
$s = curl_init();
curl_setopt($s,CURLOPT_URL,'https://affiliate-api.flipkart.net/affiliate/1.0/feeds/abhishekbh/category/7jv.json?expiresAt=1489539219049&sig=ad5ef1af7f41d68d9f8f1c1ae85e20b6');
curl_setopt($s,CURLOPT_HTTPHEADER,array('Fk-Affiliate-Id:abhishekbh', 'Fk-Affiliate-Token:2dacb05681b8481eb65291283dac2630'));
curl_setopt($s,CURLOPT_HEADER,true);
$result = curl_exec($s);
echo "<pre>";print_r($result);echo "</pre>";
PHP REST client API call
You can use file_get_contents
if the fopen wrappers are enabled. See: http://php.net/manual/en/function.file-get-contents.php
If they are not, and you cannot fix that because your host doesn't allow it, cURL
is a good method to use.
How to call REST API using CURL in php?
The issue is because you are accesing quote and author incorrectly.
Output of print_r($json_objekat)
says that:
contents is stdClass Object
quotes is array
again quotes is having
0 as index which is again stdClass Object
So, try accessing quote and author as follows:
$json_objekat->contents->quotes[0]->quote
$json_objekat->contents->quotes[0]->author
Call rest API to upload file with form data using cUrl PHP
$file = $request->file('image');
$filePath = $file->getPathName();
$type = 'image/jpeg';
$fileName = $file->getClientOriginalName();
$data = array(
'shopId' => 50,
'image' => curl_file_create($filePath, $type, $fileName)
);
$apiEndPoint='http://localhost/test/api/api/retailer/shop/update/image';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiEndPoint);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
curl_close($ch);
Related Topics
Get Name of Caller Function in PHP
How to Catch Curl Errors in PHP
Passing $_Post Values With Curl
Understanding the "Post/Redirect/Get" Pattern
Preserve Line Breaks from Textarea
How to Add/Remove Pkcs7 Padding from an Aes Encrypted String
PHP Code to Convert a MySQL Query to Csv
How to Make a PHP Soap Call Using the Soapclient Class
Show Values from a MySQL Database Table Inside a HTML Table on a Webpage
How to Enable Curl in PHP/Xampp
Preserve Line Breaks from Textarea
Max Size of Url Parameters in _Get
Login to Remote Site With PHP Curl
How to Recursively Delete a Directory and Its Entire Contents (Files + Sub Dirs) in PHP