tcdent / php-restclient Goto Github PK
View Code? Open in Web Editor NEWA generic REST API client for PHP
License: MIT License
A generic REST API client for PHP
License: MIT License
Hi Travis,
I m trying to use the RESTClient developed by you. Could you please inform if I am doing the right thing below (esp while including Authorization header):
$api = new RestClient(array(
'base_url' => "https://api2.this.com/crm/v1/rest",
'format' => "xml",
'Authorization' => 'Basic '.base64_encode("6C135EDF-C37C-4039-AEF3-5DFC079F9E6A:Statementone:sonedemo"),
'username'=> 'Statementone',
'password' => 'sonedemo',
));
Hi,
I couldn't install php-restclient using composer. Composer would always abort with the following exception message:
[Composer\RepositoryRepositorySecurityException]
The contents of http://packagist.org/p/tcdent php-restclient%24647cf0d27ae7f67bbfe60303644cfcf7c01cd45ec4f817b52f9593af108320b0.json do not match its signature.
This should indicate a man-in-the-middle attack. Try running composer again and report this if you think it is a mistake.
Actually something wrong with the signature?
Thanks, Zangue
I would like to know if you have any way to implement your library in yii2 framework, since you review a how to, where they use it but do not explain how to integrate it into the framework
Hey,
I'm using your REST client to request an API with an old fashioned router, which does not really like the fact that the router appends '.json' at the end of the URI.
I use this simple code :
// Initialize the REST Client
$params = [
'format' => 'json',
'headers' => [
'Content-Type' => 'application/json'
]
];
$this->credentials = $parameters["credentials"];
if(!empty($this->credentials["username"]) && !empty($this->credentials["password"])){
$params = array_merge($params, $this->credentials);
}
$this->uri = $parameters['uri'];
$this->api = new RestClient($params);
// Call the API ($this->uri : http://localhost:8001)
$result = $this->api->get($this->uri . "/jobs");
Is there a way to remove the '.json' appended at the end of the URI ?
Hey,
is this client php7 compatible? Can't find any info about it. Thank you.
Hi,
Is, or will, this client be PSR-7 compliant?
Hey!
Magento 2 uses the tcdent/php-restclient package and the new version (2.4.4) has a lot of PHP 8.1 incompatible issues:
Deprecated: Return type of RestClient::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/html/vendor/tcdent/php-restclient/restclient.php on line 66
PHP Deprecated: Return type of RestClient::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/html/vendor/tcdent/php-restclient/restclient.php on line 74
Deprecated: Return type of RestClient::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/html/vendor/tcdent/php-restclient/restclient.php on line 74
PHP Deprecated: Return type of RestClient::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/html/vendor/tcdent/php-restclient/restclient.php on line 70
Deprecated: Return type of RestClient::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/html/vendor/tcdent/php-restclient/restclient.php on line 70
PHP Deprecated: Return type of RestClient::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/html/vendor/tcdent/php-restclient/restclient.php on line 78
Deprecated: Return type of RestClient::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/html/vendor/tcdent/php-restclient/restclient.php on line 78
PHP Deprecated: Return type of RestClient::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/html/vendor/tcdent/php-restclient/restclient.php on line 61
Deprecated: Return type of RestClient::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/html/vendor/tcdent/php-restclient/restclient.php on line 61
PHP Deprecated: Return type of RestClient::offsetExists($key) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/html/vendor/tcdent/php-restclient/restclient.php on line 84
Deprecated: Return type of RestClient::offsetExists($key) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/html/vendor/tcdent/php-restclient/restclient.php on line 84
PHP Deprecated: Return type of RestClient::offsetGet($key) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/html/vendor/tcdent/php-restclient/restclient.php on line 90
Deprecated: Return type of RestClient::offsetGet($key) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/html/vendor/tcdent/php-restclient/restclient.php on line 90
PHP Deprecated: Return type of RestClient::offsetSet($key, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/html/vendor/tcdent/php-restclient/restclient.php on line 99
Deprecated: Return type of RestClient::offsetSet($key, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/html/vendor/tcdent/php-restclient/restclient.php on line 99
PHP Deprecated: Return type of RestClient::offsetUnset($key) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/html/vendor/tcdent/php-restclient/restclient.php on line 103
Deprecated: Return type of RestClient::offsetUnset($key) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/html/vendor/tcdent/php-restclient/restclient.php on line 103
Can i use patch verb?
can i upload file using this library on remote server???
Hi
lines 146 to 148 :
supposing
$client->options['headers'] is null
$headers is an array
then
array_merge returns null
$headers are not sent
Cheers
Hey,
It is possible / planned to cache content received into a file or with the Memcached PHP mecanism ?
https://github.com/stickfigure/blog/wiki/How-to-%28and-how-not-to%29-design-REST-APIs
Favors removing format
and suffixes from resources, which matches real world experience; it was more common a decade ago, but is rare now.
Reference other REST design patterns that may have emerged since this library was designed.
This will break parse_response() function:
http://www.jmarshall.com/easy/http/#http1.1c4
The reason is there's a "\n" between two "HTTP" info
Is it possible to post a repeatable parameter key like this?
{ "repeatableKey" : { "bla":"bla", "bla":"bla", }, "repeatableKey" : { "bla":"bla", "bla":"bla", }, "repeatableKey" : { "bla":"bla", "bla":"bla", } }
Regards,
Bas
I'm having the same problem when posting messages using Slacks api, it sometimes also return a 100 Continue before the 200 OK.
Publicación original de @pakomp en #38 (comment)
i'm newbie in PHP. I use your restclient to request as below:
<?php
require 'restclient.php';
$api = new RestClient();
$result = $api->get("https://api.github.com/repos/phatls/CallCenter_Customer/issues?state=all");
if($result->info->http_code == 200)
echo ($result->response);
else
echo "false";
?>
It works greatly. But with:
<?php
require 'restclient.php';
$api = new RestClient();
$result = $api->get("http://beka.vn/api/BkMobileLogin/GetGroup/phat.ls");
if($result->info->http_code == 200)
echo ($result->response);
else
echo "false";
?>
It returns false. Please help me if you don't have any inconvenient.
thanks
There is POST method that has not been working.
This is sample CURL example at the service.
{"email": "[email protected]", "items": [{ "name": "api Name", "quantity": 10, "unit_price": 2}, { "name": "api 2", "quantity": "4", "unit_price": 3 }]
}
Have the tried the following.
$data = array(
'email' => '[email protected]',
);
$data['items'] = array(
'name' => 'fruits', 'quantity' => 4,
'unit_price' => 7,
);
$data2 = array(
'email' => '[email protected]', 'currency' => 'USD',
'country' => 'US', 'billing_first_name' => 'John',
// 'items' => '',
'items' => array(
0 => array(
'unit_price' => 10.00,
'name' => 'item1',
'quantity' => 15
),
1 => array(
'unit_price' => 34.00,
'name' => 'item2',
'quantity' => 42
)
)
);
$result = $api->post('invoice/', $data,
array('Content-Type' => 'application/json'));
The service just accepts email, and ignores items.
All POST
type queries to some private APIs return the result as "The requested URL was rejected. Please consult with your administrator". I found that the following section on line 151 caused this:
$curlopt[CURLOPT_HTTPHEADER][] = sprintf("%s:%s", $key, $value);
Putting a space after the semicolon fixes the problem.
$curlopt[CURLOPT_HTTPHEADER][] = sprintf("%s: %s", $key, $value);
Hello , how I can't retrieve the only the body response?
I don't find how to do in your Documentation.
Hi, I want to know how I can avoid this error.
SSL certificate problem: self signed certificate
Currently the API that is a consumer used by HTTPS but the SSL certificate is not yet valid, I want to be able to use its library without having a valid SSL.
How could I solve the error?
RestClient
is dynamic and does not conform to API specs. Provide an interface to defining API methods for documentation and validation of client implementations.
It'd be great if you could, once the request is totally built, and right before performing it, mark a flag to output the curl as in cli to be able to debug & compare... just saying :D
Hi im trying it link a WooCommerce website with PromoSoftware
http://www.promosoftware.co.uk/rest-api/
I have never done a API before and am just looking for some pointers to help me post new customers name etc to Promosoftware...
Thanks
First, I love the class you created. It saved me a lot of time creating my own custom clients. I noticed a problem working with the AirWatch REST API. The API return headers such as "200 Continue" when data sets are abnormally large or truncated. So, you end up with two HTTP headers. The two headers are separated by line break.
HTTP/1.1 100 Continue
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/7.5
The result of this is that you never get the response parsed correctly. I did a bit of a hack to check for a 200 code and then skip it and the next line. I'm sure this could be done better.
public function parse_response($response){
$headers = array();
$http_ver = strtok($response, "\n");
// Check for 100 Continue header followed by 200 OK header
if (preg_match("/^HTTP\/[0-9]\.[0-9] 100 Continue/", $http_ver)) {
$this->continue = true;
// Skip blank line and get next http
$line = strtok("\n");
$http_ver = strtok("\n");
}
while($line = strtok("\n")){
if(strlen(trim($line)) == 0) break;
list($key, $value) = explode(':', $line, 2);
$key = trim(strtolower(str_replace('-', '_', $key)));
$value = trim($value);
if(empty($headers[$key]))
$headers[$key] = $value;
elseif(is_array($headers[$key]))
$headers[$key][] = $value;
else
$headers[$key] = array($headers[$key], $value);
}
$this->headers = (object) $headers;
$this->response = trim(strtok(""));
}
Hey,
I'm not really sure about this, but I don't think this Java REST GUI is really related to your PHP library.
I looked at your wiki and it seems that everyone is able to write pages to this Wiki. Maybe should you remove those permissions :)
Hi,
I'm getting an error when I try to send a PDF file as a parameter with other information.
This problem disappears when I use raw cURL.
Cheers.
We currently have incomplete typing in order to support older versions of PHP. Determine lowest version cutoff to support all type features & users.
Currently, the format_query() method is not compatible to multi-dimensional parameter arrays.
Hi,
If you set format eg. to "json", the execute function always appends it as file extension to the url because of this:
if($client->options['format']) $client->url .= '.'.$client->options['format'];
There should be an option to disable that. In my case, the service doesn't accept any file extension. If I leave format empty I get "Response format could not be determined.", which indicates that get_response_format couldn't auto detect the response format. Even if it would auto detect, I wouldn't want to rely on that, because the service response headers might change although the response content is still the same.
Just a boolean option and a check in the if statement above should fix that.
Thanks for creating this very helpful class!
This is a build on issue #14.
I need to connect to an API that is responding with HTML header information in between the HTTP 100 Continue and the HTTP 200 OK response lines. This results in the $api->header info being incomplete, the $api->response field containing header information and my xml decoder failing for multiple reasons.
I've recommended a potential fix for this issue. Could the parse_response method be updated to handle this peculiar case?
-- Problematic Header --
HTTP/1.1 100 Continue
Via: 1.1 ID-0000605166710030 uproxy-3
HTTP/1.1 200 OK
Date: Wed, 24 Oct 2018 18:20:07 GMT
Accept-Ranges: bytes
Server: Apache
Content-Type: text/xml
Transfer-Encoding: chunked
Connection: close
Via: 1.1 ID-0000605166710030 uproxy-2
-- Potential Fix -- see 3 lines with $http_continue
public function parse_response($response){
$headers = [];
$this->response_status_lines = [];
$http_continue=false;
$line = strtok($response, "\n");
do {
if(strlen(trim($line)) == 0){
// Since we tokenize on \n, use the remaining \r to detect empty lines.
if((count($headers) > 0) && (!$http_continue)) break; // Must be the newline after headers, move on to response body
}
elseif(strpos($line, 'HTTP') === 0){
// One or more HTTP status lines
$this->response_status_lines[] = trim($line);
//HANDLE HTTP 100 Continue
$http_continue = (strpos($line, '100 Continue') !== false );
}
tcdent/php-restclient/restclient.php:268
Using ${var} in strings is deprecated, use {$var} instead
Hi,
I noticed that $url is not a defined property of the RestClient class. However it is used implicitly e.g. here
Is there a reason for this? I will gladly create a pull request, if it is not.
The reason I noticed is, that I extend the RestClient class and overwrite the __set and __get functions, which are being called when setting the url. I could also just test for the property name and make an exception in my code. Just curious, why it is not defined explicitly, while all other properties are.
Also maintain the ability to create faked requests with the _method
parameter.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.