So my client needs a REST API using PHP that provides output as per the conditions on the URL parameters
So now there are three URL's basically which is currently needed and they are done.
so they are
localhost/newapi/client/<AuthKey> - for authorizing
localhost/newapi/client/<clientid>/categories/ - to get all the categories
localhost/newapi/client/<clientid>/categories/<categoryid> - to get all items in a category
used .htaccess for fancy URL
So now he requested that AuthKey need to be added to HTTP header not the URL. So the AuthKey must be passed as header and the rest as URL parameters
So my question is how this can be done. and how to retrieve the AuthKey from the request?
Any tutorials or comments regarding this question is welcome
you can tell the client when he request your api he add a header as below:
AuthKey: your-api-auth-key
or
Token: your-api-token
and then in your php code make
$headers = getallheaders();
$token = $headers['Token'] or ['AuthKey'];
then you check if the key in database and then process your code
Note:
your client can add Header with PHP cURL
curl_setopt($curl-handle, CURLOPT_HEADER, array(
'Token' => 'client-auth-token', //or
'AuthKey' => 'client-auth-token'
));
you can use this code to connect with rest in php
$url = 'localhost/newapi/client/';
$opts = array('http' =>
array(
'method' => 'POST',
'header' => "Content-Type: application/json\r\n"."Authorization: Basic ".<authkey>."\r\n",
'content' => $data,
'timeout' => 60
)
);
$context = stream_context_create($opts);
$result = file_get_contents($url, false, $context, -1, 40000);
return $result;
Related
CoinMarketCap Api offer a lot of data by making calls to different links. Every time you make a call it cost 1 credit and of course, if that call return 5,000 coins then it cost 25 credits. So, I can't make call to different link every minute. How can I make a call to at least 4 links such as:
https://pro-api.coinmarketcap.com/v1/cryptocurrency/trending/latest
https://pro-api.coinmarketcap.com/v1/cryptocurrency/trending/gainers-losers
https://pro-api.coinmarketcap.com/cryptocurrency/listings/latest?limit=5000
https://pro-api.coinmarketcap.com/v2/cryptocurrency/info
and here is the code that CoinMarketCap offer and it works (tested):
$url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest';
$parameters = [
'start' => '1',
'limit' => '5000',
'convert' => 'USD'
];
$headers = [
'Accepts: application/json',
'X-CMC_PRO_API_KEY: *********-****-****-****-***********'
];
$qs = http_build_query($parameters); // query string encode the parameters
$request = "{$url}?{$qs}"; // create the request URL
$curl = curl_init(); // Get cURL resource
//Set cURL options
curl_setopt_array($curl, array(
CURLOPT_URL => $request, // set the request URL
CURLOPT_HTTPHEADER => $headers, // set the headers
CURLOPT_RETURNTRANSFER => 1 // ask for raw response instead of bool
));
$response = curl_exec($curl); // Send the request, save the response
print_r(json_decode($response)); // print json decoded response
curl_close($curl); // Close request
you can use the curl_multi_init to handle the multiple curl request asynchronously. you can read here more about multi curl
I am very new to php so this question might be trivial.
I am trying to understand if it is possible to redirect the browser from php server side to the page returned by HTTP request.
I have a HTTP Post request looking like so:
$postdata = http_build_query(
array(
"someData" => "data" ,
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'content' => $postdata,
'header' =>
"Cookie: someCookie" .
'content-type: application/x-www-form-urlencoded; charset=utf-8'
)
);
$context = stream_context_create($opts);
$result = fopen('myWebsiteUrl', 'r', false, $context);
var_dump(stream_get_contents($result));
In the post I am being redirected to a different page with Get.
I am trying to force the browser to move to the 'redirected' page. With the above code I am getting back the html of the redirected page but what I'm after is an actual redirect.
The option of retrieving the redirect URL and doing the redirect myself in PHP doesn't work because the Get request has to happen within the same session as the Post.
header('Location: whereveryouwantogo.php?get=123456');
does a redirect for you with a GET-parameter attached. I'm not sure if I really got your question right, but I'm pretty sure you could do what you want/need with the header-command.
I wanted to send a post request to pilosa database. The request is like this -
curl localhost:10101/index/user/query
-X POST
-d 'Bitmap(frame="language", id=5)'.
How can i send the following request through php ?
Link for referrence : https://www.pilosa.com/docs/api-reference/
If you don't have the php curl library available to you, you can query Pilosa with php's file_get_contents which is part of core php. The following php script should perform your example query:
<?php
$url = 'http://localhost:10101/index/user/query';
$data = 'Bitmap(frame="language", id=5)';
$options = array(
'http' => array(
'method' => 'POST',
'content' => $data
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
var_dump($result);
?>
The Pilosa HTTP API documentation can be found at: https://www.pilosa.com/docs/api-reference/
Need to request some code two times in my app. Fist request url as ajax call, and also need to request this url in controller (something like hmvc). I know how to develop this via curl but I found another kind of idea how to implement this, just use function file_get_contents with before prepared params. This my code:
// Setup limit per page
$args['offset'] = $offset;
$args['limit'] = $this->_perpage;
// --
// Convert search arguments to the uri format
$data = http_build_query($args);
// Define request params
$options = array(
'http' => array(
'header' => 'Content-type: application/json' . PHP_EOL .
'Content-Length: ' . strlen($data) . PHP_EOL,
'method' => 'POST',
'content' => $data,
),
);
$context = stream_context_create($options);
$result = file_get_contents(
'http://'.$_SERVER['HTTP_HOST'].'/search/items', FALSE, $context
);
Request method was detected ok in requested uri, but params wasn't passed. Why this is not pass arguments to request? Where is bug in my code? Many thanks for any answers.
http_build_query builds application/x-www-form-urlencoded content. (not application/json)
There is a full example:
How to post data in PHP using file_get_contents?
Content type should be application/x-www-form-urlencoded. If you want to stay with application/json, try to get posted data using file_get_contents("php://input").
$post_data = array(
'url' => $all[2],
'op' => 'sv',
'sid' => 1
);
// Send a request to example.com
$result = post_request('http://www.yahoo.com', $post_data);
function PostRequest($url) {
$opts = array('http' =>
array(
'method' => 'GET',
'header' => "Content-type: application/x-www-form-urlencoded\r\n"."Accept-language: en\r\n" .
"Cookie: member_id=8593099\r\n" .
"Cookie: pass_hash=fad917fe75e1059f85fc6d9bb6f7a19f\r\n".
"Cookie: session_id=279fe56fd87e5371dc7e1c9f66c27522"
)
);
$context = stream_context_create($opts);
$result = file_get_contents($url, false, $context);
return $result;
}
I am able to send the request, but my action needs login to be performed.
Even once I'm logged in, it classifies me as not logged in.
I'm using localhost to send out the request. Is that because of the different domain?
I already copied the login cookies for my localhost, but it is still not working.
Any ideas?
What I tried to do is send http request with php.
My request has sent out, but my destination cannot detect cookies, and claim I am not login.
I'm not too sure what you're trying to accomplish, as the code you've posted isn't quite clear. post_request() isn't a native PHP-function, so you'd have to give us a sample of it for us to be able to help you further.
I would however recommend that you've check that you've put session_start(); way up top in your PHP-files - it ensures that you're able to access the session/cookie.