guzzle

Retrieve the whole XML response body with Guzzle 6 HTTP Client

杀马特。学长 韩版系。学妹 提交于 2019-12-03 18:03:38
问题 I wanted to use Guzzle 6 to retrieve an xml response from a remote API. This is my code: $client = new Client([ 'base_uri' => '<my-data-endpoint>', ]); $response = $client->get('<URI>', [ 'query' => [ 'token' => '<my-token>', ], 'headers' => [ 'Accept' => 'application/xml' ] ]); $body = $response->getBody(); Vardumping the $body would return a GuzzleHttp\Psr7\Stream object: object(GuzzleHttp\Psr7\Stream)[453] private 'stream' => resource(6, stream) ... ... I could then call $body->read(1024)

Posting a file to a web service with Guzzle

佐手、 提交于 2019-12-03 15:35:07
I've been trying for several hours to make a POST request that sends a file. First tried a simple file_get_contents() with stream context, doesn't seem to work. I never get a response back while the GET on a different URL works. I searched on the web for an HTTP client and found Guzzle which was downloaded 400k times on Packagist; I decided to try out this technology. Well documented but, alas, getting an error also when posting that damn file. $request = $client ->post('/files/') ->addPostFields(array('comments' => 'no value')) ->addPostFile('file', 'test.doc') ->setAuth($this->username,

What is the best way to use Guzzle to check if a remote file exists?

落花浮王杯 提交于 2019-12-03 14:48:28
I would like to use Guzzle to check if a remote file exists. This is an example of how I am currently checking: /** * @return boolean */ function exists() { // By default get_headers uses a GET request to fetch the headers. // Send a HEAD request instead stream_context_set_default( array( 'http' => array( 'method' => 'HEAD' ) ) ); // Get the file headers $file_headers = @get_headers($this->file); // Check file headers for 404 if($file_headers[0] == 'HTTP/1.1 404 Not Found') return false; // File not available. return true; // File is available! } However, since I'm already using Guzzle

How do I send parameters for a PUT request in Guzzle 5?

扶醉桌前 提交于 2019-12-03 13:44:32
I have this code for sending parameters for a POST request, which works: $client = new GuzzleHttp\Client(); $request = $client->createRequest('POST', 'http://example.com/test.php'); $body = $request->getBody(); $request->getBody()->replaceFields([ 'name' => 'Bob' ]); However, when I change POST to PUT, I get this error: Call to a member function replaceFields() on a non-object This is because getBody is returning null. Is it actually correct to send PUT parameters in the body? Or should I do it in the URL? According to the manual , The body option is used to control the body of an entity

How to ignore invalid SSL certificate errors in Guzzle 5

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-03 11:29:36
问题 This should be an easy thing to do. I can find plenty of references to how to do it in Guzzle 3, but they don't work in Guzzle 5. What I am doing so far: $this->client = new GuzzleClient(['defaults' => [ 'verify' => 'false' ]]); When I send a request though I get this error: RequestException in RequestException.php line 51: SSL CA bundle not found: false I cannot find any useful reference to this error on google. If I could get access to the curl options then I could try something like the

Using Guzzle to consume SOAP

…衆ロ難τιáo~ 提交于 2019-12-03 10:55:46
I'm loving the Guzzle framework that I just discovered. I'm using it to aggregate data across multiple API's using different response structures. It's worked find with JSON and XML, but one the services i need to consume uses SOAP. Is there a built-in way to consume SOAP services with Guzzle? You can get Guzzle to send SOAP requests. Note that SOAP always has an Envelope, Header and Body. <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Header/> <soapenv:Body>

Proper way to send (POST) xml with guzzle 6

和自甴很熟 提交于 2019-12-03 10:42:13
I want to perform a post with guzzle sending an xml file. I did not find an example. What I 've done so far is : $xml2=simplexml_load_string($xml) or die("Error: Cannot create object"); use GuzzleHttp\Client; use GuzzleHttp\Psr7\Request; $client = new Client(); // $request = new Request('POST', $uri, [ 'body'=>$xml]); $response = $client->send($request); // //$code = $response->getStatusCode(); // 200 //$reason = $response->getReasonPhrase(); // OK // echo $response->getBody(); No matter what I try I get back error -1 which means that xml is not valid. XML that I send passes online validation

Copy remote file using Guzzle

倾然丶 夕夏残阳落幕 提交于 2019-12-03 06:59:33
问题 I'm trying to copy a remote file (image PNG, GIF, JPG ...) to my server. I use Guzzle since I sometimes get 404 with copy() even if the file exists and I also need to do a basic auth. This script is within a long script launched in command triggered by a cron job. I'm pretty new to Guzzle and I successfully copy the image but my files have wrong mime type. I must be doing something wrong here. Please suggest me a good way to do this (including checking success/failure of copy and mime type

PHP: How to check for timeout exception in Guzzle 4?

纵饮孤独 提交于 2019-12-03 06:05:20
Guzzle throws an exception if an error occured during the request. Unfortunately, there does not seem to be an error specific to timeouts - which is important for me as I know that those can ocassionally occur. I'd like to retry the corresponding request and need to able to tell if the error occured due to a timeout. From the docs : // Timeout if a server does not return a response in 3.14 seconds. $client->get('/delay/5', ['timeout' => 3.14]); // PHP Fatal error: Uncaught exception 'GuzzleHttp\Exception\RequestException' The RequestException has the info in its message property: "cURL error

PHP - Why Use Guzzle Instead of cURL?

别来无恙 提交于 2019-12-03 04:08:14
问题 In my application, I originally began using cURL to retrieve data from various APIs. Today, I tried using Guzzle to complete the same task. So far, both cURL and Guzzle seem to work equally good. Judging by Github, a lot of people seem to like Guzzle, but I don't really appreciate why. My question: For my situation (retrieving data from various APIs), is it preferable to use Guzzle? Will I eventually come to regret it, if I use cURL instead Guzzle (or vice versa)? I am using PHP / Laravel.