My cURL request looks like:
http://httpbin.org/post -d '{"multifilter":{"limit":5}}'
My Guzzle code:
$request = $client->createRequest('POST', 'http://httpbin.org/post');
$postBody = $request->getBody();
$postBody->setField('multifilter', array("limit"=>"5"));
$response = $client->send($request);
This line
$postBody->setField('multifilter', array("limit"=>"5"));
is not correct, but how to set variable as array ?
Or maybe exist something to add json query ? I mean something like addJsonquery('multifilter":{"limit":5}}') ?
solution is add json to body
$request = $client->createRequest('POST', $url, [
'body' => $json,
]);
$response = $client->send($request);
Answer in github:
github.com/guzzle/guzzle/issues/1027
Related
I am trying to replicate the same json request in php from this Python code:
import requests
url = 'http://site.api.espn.com/apis/site/v2/sports/football/college-football/scoreboard'
payload = {
'limit':'500',
'groups':'8'}
jsonData = requests.get(url, params=payload).json()
I know how to curl a API request in php but not sure the sythanx for the parameter input, in this case , 'limit' & 'groups'
Use http_build_query() to convert the array of parameters to URL query parameters, then concatenate it to the URL.
file_get_contents() is the simple equivalent to requests.get() if you don't need to provide any custom headers.
$payload = [
'limit' => 500,
'groups' => 8
];
$params = http_build_query($payload);
$url = 'http://site.api.espn.com/apis/site/v2/sports/football/college-football/scoreboard?' . $params;
$result = file_get_contents($url);
$json_data = json_decode($result, true);
I tried my first API call but something is still wrong. I added my API-Key, choose the symbol and tried to echo the price. But it is still not valid. But my echo is still 0. Maybe someone show me what i did wrong. Thank you!
<?php
$coinfeed_coingecko_json = file_get_contents('https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?symbol=ETH');
$parameters = [
'start' => '1',
'limit' => '2000',
'convert' => 'USD'
];
$headers = [
'Accepts: application/json',
'X-CMC_PRO_API_KEY: XXX'
];
$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
$coinfeed_json = json_decode($coinfeed_coingecko_json, false);
$coinfeedprice_current_price = $coinfeed_json->data->{'1'}->quote->USD->price;
?>
<?php echo $coinfeedde = number_format($coinfeedprice_current_price, 2, '.', ''); ?>
API Doc: https://coinmarketcap.com/api/v1/#operation/getV1CryptocurrencyListingsLatest
There is a lot going on in your code.
First of all $url was not defined
Second of all you made two requests, one of which I have removed
Third; you can access the json object by $json->data[0]->quote->USD->price
Fourth; I removed the invalid request params
I have changed a few things to make it work:
$url = "https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest";
$headers = [
'Accepts: application/json',
'X-CMC_PRO_API_KEY: ___YOUR_API_KEY_HERE___'
];
$request = "{$url}"; // 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
$json = json_decode($response);
curl_close($curl); // Close request
var_dump($json->data[0]->quote->USD->price);
var_dump($json->data[1]->quote->USD->price);
We are using the MTN PHP-SDK from the below URL
https://github.com/digitalmaterial/dep.api.auth.php
Code:
$path = '/subscription/' . $subscription_id;
$depClient = new MTNDEP\DEPClient($accessKey, $accessSecret, $apiKey, $baseUrl);
$requestBody = [];
$response = $depClient->createRequest(MTNDEP\DEPClient::DELETE, $path, null, $requestBody)->send();
$responseArray = json_decode((string) $response->getBody(), true);
Response:
Client error: `DELETE https://api.dep.mtn.co.za/subscription/` resulted in a `404 Not Found` response:
{"message":"No method found matching route subscription/ for http method DELETE."}
We are unable to find any MTN-DEP Cancel subscription PHP code. Please help us and let us know how can we delete the subscription from MTN end?
i have noticed 2 things :
1- in the mentioned documentation, the request sent like this
$depClient->createRequest('POST' '/subscription');
so i think you need to use this , please try to adhere with all steps
$depClient->createRequest('DELETE',$path); // Returns DEPClient object for chaining, see below
$depClient->getRequest(); // Will return the GuzzleHttp\Psr7\Request object with signed auth details for DEP API requests
$response = $depClient->send();
$statusCode = $response->getStatusCode(); // returns the http status code
$rawResponse = (string) $response->getBody(); // body, you will need to cast to string or echo to get the body data.
$responseArray = json_decode($rawResponse, true); // return json decode array.
2- make sure the $subscription_id is set and have a value before you send the request.
Stating to the documentation, you need to specify the customer ID and the subscription ID.
You have to make a DELETE request to the endpoint /customers/{id}/subscriptions/{subscriptionId}.
Maybe the path specified in your code should look like this:
$path = '/customers/' . $customer_id . '/subscriptions/' . $subscription_id;
$depClient = new MTNDEP\DEPClient($accessKey, $accessSecret, $apiKey, $baseUrl);
$requestBody = [];
$response = $depClient->createRequest(MTNDEP\DEPClient::DELETE, $path, null, $requestBody)->send();
$responseArray = json_decode((string) $response->getBody(), true);
I am trying to parse this url:
https://halykbank.kz/presscenter/novosti
The news are loaded by AJAX. In network I found URL, that must show loaded news, but there only
{"result":false,"hint":"NO_AUTHORIZATION_DATA"}
Why I am getting this? Here the URL, that I think must show me loaded news:
https://backend.halykbank.kz/struct/category-items?categoryId=199&sort=position%20desc&offset=200&limit=100
You must send these header
Auth-User-Id, Auth-Token, Auth-Time
composer require guzzlehttp/guzzle:~6.0
$client = new GuzzleHttp\Client();
$res = $client->get('https://api.github.com/user', ['auth' => ['user', 'pass']]);
echo $res->getStatusCode(); // 200
$json_string = $res->getBody();
$json_obj = json_decode($json_string); //return Object<BR>
$json_arr = json_decode($json_string,true); //return Array
I need to convert a cURL command like this into Guzzle-way:
curl -XPOST "https://api/v1.0/endpoint" -F "file=#img.jpg"
This is what I'm trying so far:
$httpClient = new \GuzzleHttp\Client;
$req = $httpClient->createRequest('POST', $url), []);
$postBody = $req->getBody();
$postBody->addFile(new \GuzzleHttp\Post\PostFile('photo', fopen(storage_path() . '/' . $filename, 'r')));
$response = $httpClient->send($req);
But I'm not getting the same response as with the previous command, which is something like:
{"id":5378678,"url":"ui/54/68/97/24/img.jpg"}
I'm getting a GuzzleHttp\Message\Response object, but I'm not being able to find the id and url attributes in there.
Any help will be appreciated!
Just noticed I could do $response->json() to get what I wanted.