Does anyone know how to extract HTML/plain text response from Guzzle HTTP client? If you look at the example below, we can get xml and json response content easily but I don't know how to get plain text or HTML response string.
Documentation didn't have an option for plain text or HTML unlike json() and xml().
_Example_
$client = new Client($base_url);
$request = $client->createRequest($method, $uri, null, $this->requestPayload);
$response = $client->send($request);
$xml = $response->xml(); // For XML response
$json = $response->json(); // For JSON response
$html = $response->????????(); // For plain text or HTML response
Response for HTML/plain text is shown below so I need to get last line which is success 202 OK! Micky Mouse
HTTP/1.1 202 Accepted
Date: Sun, 02 Nov 2014 13:23:52 GMT
Server: Apache/2.4.7 (Ubuntu)
X-Powered-By: PHP/5.5.9-1ubuntu4.4
Cache-Control: no-cache
X-Debug-Token: 252f33
X-Debug-Token-Link: /local/restful/web/app_dev.php/_profiler/252f33
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8
success 202 OK! Micky Mouse
Solution:
$response->getBody(true);
$response->getBody(true);
Output :
Stream {#337 ▼
-stream: :stream {@10 ▼
wrapper_type: "PHP"
stream_type: "TEMP"
mode: "w+b"
unread_bytes: 0
seekable: true
uri: "php://temp"
options: []
}
-size: null
-seekable: true
-readable: true
-writable: true
-uri: "php://temp"
-customMetadata: []
}
Any help ?
Try just $response->getBody() or (string)$response->getBody().
use $response->getBody()->getContents()
I do that $response->getBody()->getContents() but result returns with b" text. Why doing that.
b"[{"detay":""}]"
From what I understand, the b means bytes literal and it needs to be decoded
$data = $response->getBody()->getContents();
return collect(json_decode(utf8_decode($data)));
utf8_decode removes the 'b'. collect/json_decode just help convert it to a laravel collection
how can i access this multipart response in (PHP laravel) anyone can help?
b"""
--myboundary
Content-Type: text/plain
Content-Length: 4147
Events[0].Action=4
Events[0].Code=TrafficJunction
Events[0].CommInfo.Country=
Events[0].CommInfo.ParkType=0
Events[0].CountInGroup=1
Events[0].DetectRegion[0][0]=3380
Events[0].DetectRegion[0][1]=3139
Events[0].DetectRegion[1][0]=3695
Events[0].DetectRegion[1][1]=5869
Events[0].DetectRegion[2][0]=5140
Events[0].DetectRegion[2][1]=6033
Events[0].DetectRegion[3][0]=5090
Events[0].DetectRegion[3][1]=2822
Events[0].EncodeSequence=0
Events[0].EventBaseInfo.Code=TrafficJunction
Events[0].EventBaseInfo.Index=0
Events[0].FrameSequence=2867908
Events[0].GroupID=12767
Events[0].IndexInGroup=1
Events[0].JunctionDirection=Reverse
Events[0].Lane=1
Events[0].Mark=0
Events[0].Name=TrafficJunction1
Events[0].Object.Action=Appear
Events[0].Object.BoundingBox[0]=6024
Events[0].Object.BoundingBox[1]=7400
Events[0].Object.BoundingBox[2]=6392
Events[0].Object.BoundingBox[3]=7816
Events[0].Object.Category=Unknown
Events[0].Object.Center[0]=6208
Events[0].Object.Center[1]=7608
Events[0].Object.Confidence=26
Events[0].Object.Country=
Events[0].Object.Image.Height=80
Events[0].Object.Image.Length=2614
Events[0].Object.Image.Offset=490745
Events[0].Object.Image.Width=128
Events[0].Object.MainSeat.DriverCalling=unknow
Events[0].Object.MainSeat.DriverSmoking=unknow
Events[0].Object.MainSeat.SafeBelt=unknow
Events[0].Object.ObjectID=16363
Events[0].Object.ObjectType=Plate
Events[0].Object.OriginalBoundingBox[0]=1411
Events[0].Object.OriginalBoundingBox[1]=1039
Events[0].Object.OriginalBoundingBox[2]=1498
Events[0].Object.OriginalBoundingBox[3]=1094
Events[0].Object.RelativeID=16361
Events[0].Object.ShotFrame=true
Events[0].Object.SlaveSeat.DriverCalling=unknow
Events[0].Object.SlaveSeat.DriverSmoking=unknow
Events[0].Object.SlaveSeat.SafeBelt=unknow
Events[0].Object.Speed=0
Events[0].Object.Text=6BA
Events[0].Object.TrackType=0
Events[0].ObjectID=16361
Events[0].OpenStrobeState=Close
Events[0].PTS=43064085660.000002
Events[0].RuleId=0
Events[0].Sequence=1
Events[0].SerialNo=
Events[0].SnapSequence=2381
Events[0].SnapSourceCombine=false
Events[0].Source=34
Events[0].Speed=0
Events[0].TrafficCar.BlackList.Enable=false
Events[0].TrafficCar.CapTime=1595843541.092000
Events[0].TrafficCar.CarType=NormalCar
Events[0].TrafficCar.CountInGroup=1
Events[0].TrafficCar.Country=
Events[0].TrafficCar.DefendCode=WnLThd1tXJNf5PZ7
Events[0].TrafficCar.Direction=0
Events[0].TrafficCar.DrivingDirection[0]=Leave
Events[0].TrafficCar.DrivingDirection[1]=
Events[0].TrafficCar.DrivingDirection[2]=
Events[0].TrafficCar.Event=TrafficJunction
Events[0].TrafficCar.GroupID=12767
Events[0].TrafficCar.IndexInGroup=1
Events[0].TrafficCar.Lane=1
Events[0].TrafficCar.LowerSpeedLimit=20
Events[0].TrafficCar.MachineAddress=
Events[0].TrafficCar.MachineName=ITC215-PW4I-IRLZF27135
Events[0].TrafficCar.PlateColor=Unknow
Events[0].TrafficCar.PlateNumber=6BA
Events[0].TrafficCar.PlateType=Unknown
Events[0].TrafficCar.RoadwayNo=
Events[0].TrafficCar.RouteNo=
Events[0].TrafficCar.Speed=0
Events[0].TrafficCar.UpperSpeedLimit=70
Events[0].TrafficCar.VehicleColor=Unknow
Events[0].TrafficCar.VehicleSign=
Events[0].TrafficCar.VehicleSize=Light-duty
Events[0].TrafficCar.WhiteList.Enable=false
Events[0].TriggerType=2
Events[0].UTC=1595843541
Events[0].UTCMS=92
Events[0].Vehicle.Action=Appear
Events[0].Vehicle.BoundingBox[0]=4208
Events[0].Vehicle.BoundingBox[1]=2856
Events[0].Vehicle.BoundingBox[2]=7280
Events[0].Vehicle.BoundingBox[3]=8168
Events[0].Vehicle.Category=Microbus
Events[0].Vehicle.Center[0]=5744
Events[0].Vehicle.Center[1]=5512
Events[0].Vehicle.Confidence=194
Events[0].Vehicle.HeadDirection=2
Events[0].Vehicle.ObjectID=16361
Events[0].Vehicle.ObjectType=Vehicle
Events[0].Vehicle.OriginalBoundingBox[0]=986
Events[0].Vehicle.OriginalBoundingBox[1]=440
Events[0].Vehicle.OriginalBoundingBox[2]=1706
Events[0].Vehicle.OriginalBoundingBox[3]=1140
Events[0].Vehicle.RelativeID=16363
Events[0].Vehicle.ShotFrame=true
Events[0].Vehicle.Speed=0
Events[0].Vehicle.Text=
Events[0].VehicleHeadDirection=2
Events[0].ViolationSnapSource=3
--myboundary
Content-Type: image/jpeg
Content-Length: 493359
Ï Ó\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00 █\x00┼\x00\x03\x02\x02\x03\x02\x02\x03\x03\x03\x03\x04\x03\x03\x04\x05\x08\x05\x05\x04\x04\x05
\x07\x07\x06\x08\f
\f\f\v
\v\v\x0E\x12\x10\x0E\x11\x0E\v\v\x10\x16\x10\x11\x13\x14\x15\x15\x15\f\x0F\x17\x18\x16\x14\x18\x12\x14\x15\x14\x01\x02\x02\x02\x02\x02\x02\x05\x03\x03\x05
\x07\x06\x07
"""
Most helpful comment
use $response->getBody()->getContents()