Wednesday, November 20, 2013

How to use ZF2's Curl Http Adapter with Header information

Just sharing some of my painful WTF experiences using ZF2's Curl Http client adapter so you wont have to deal with my headaches.

Basically I am trying to send a CURL_HTTPHEADER with these params.
This is what it should look like straight php:

$c = curl_init('http://url');
curl_setopt($c, CURLOPT_POST, 1);
curl_setopt($c, CURLOPT_POSTFIELDS, $data);
curl_setopt($c, CURLOPT_HTTPHEADER, array(
    'Content-type: application/json',
    'Authorization: Bearer 1jo41324knj23o'
));
Simple enough right? Well I was assuming the same thing using ZF2's Curl Adapter:
$client = new Client('http://url');
$client->setMethod('post');

$adapter = new Curl()
$adapter->setCurlOption(CURLOPT_POST, 1);
$adapter->setCurlOption(CURLOPT_POSTFIELDS, $data);
$adapter->setCurlOption(CURLOPT_HTTPHEADER, array(
    'Content-type: application/json',
    'Authorization: Bearer 1jo41324knj23o'
));
Well the thing is this will not work because the headers and data are being set exclusively by write() in the client object as you will see in the source:
// inside Curl::write()
// line 374 Curl.php
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $curlHeaders);

// line 380 Curl.php
if ($method == 'POST') {
    curl_setopt($this->curl, CURLOPT_POSTFIELDS, $body);
}

// line 398 Curl.php
if (isset($this->config['curloptions'])) ...
    foreach ((array) $this->config['curloptions'] as $k => $v) ...
 
Notice at line 398 that this is where the remaining curl parameters gets set. This means that CURLOPT_HTTPHEADER and CURLOPT_POSTFIELDS have already been defined so our usage from above will not work (Maybe there is a CURL flag to overwrite this, I just don't know at the moment). So how do you make this work? You have to pass your definitions in the Client object:
$client = new Client('http://url');
$client->setMethod('post');
$client->setRawBody($data);
$client->setHeaders(array(
    'Content-Type: application/json',
    'Authorization: Bearer 1jo41324knj23o',
));
$client->setAdapter(new Curl());
$client->send();
Now the write() method will pull from these params when assembled

27 comments:

Unknown said...

Thanks so much for sharing.. had a lot of WTF moments myself before I found your post! :)

Unknown said...
This comment has been removed by a blog administrator.
Unknown said...

tommy hilfiger outlet
coach outlet
longchamp handbags
calvin klein underwear
the north face jackets
cheap nfl jerseys
moncler coats
michael kors uk
air jordan 13
jordan 4
coach outlet online
swarovski jewelry
los angeles lakers jerseys
minnesota vikings jerseys
nhl jerseys
chelsea jersey
cheap oakley sunglasses
swarovski crystal
oakley sunglasses
fitflop
tods shoes
burberry handbags
louis vuitton outlet
ray ban uk
barcelona jersey
mont blanc
michael kors handbags
oakland raiders jerseys
gucci outlet
tods outlet
nike trainers
custom mlb jerseys
iphone 6 plus cases
chicago blackhawks jersey
true religion jeans
michael kors outlet
kate spade outlet
ghd
michael kors handbags
tiffany jewelry
2015717caihauli

Unknown said...

2015-09-16 zhengjx
nike trainers
toms outlet
celine handbags
michael kors outlet
Hollister UK Clothing Store
Louis Vuitton Outlet Discount Handbags
Cheap Louis Vuitton Handbags Luggage Bags Sale
cheap jordans
hollister uk
michael kors bags
ugg boots sale
Gucci Outlet Online 80% OFF
ugg australia
michael kors outlet
Mont Blanc Pens For Sale
Michael Kors Outlet Online 70% Off Handbags
Ralph Lauren UK Sale Polo Shirts
Christian Louboutin Outlet For Women
ugg boots
Louis Vuitton Outlet Authentic Handbags Discount
michael kors handbags
Louis Vuitton Handbags Outlet On Sale
Louis Vuitton Outlet Purses and Handbags For Sale
canada goose
hermes birkin
New Jordans Cheap For Sale
Abercrombie & Fitch Clothing With Big Discount
Coach Factory Outlet Handbags 70% OFF
Lebron 12,11,10 Shoes For Sale
cheap uggs sale

Unknown said...

ray ban sunglasses, http://www.raybansunglassesonline.us.com/
nike trainers, http://www.niketrainers.me.uk/
gucci handbags, http://www.guccihandbags-outlet.co.uk/
herve leger, http://www.herveleger.us.com/
timberland boots, http://www.timberlandboots.name/
coach outlet online, http://www.coachoutletonline-store.us.com/
michael kors outlet, http://www.michaelkorsoutletcanada.in.net/
juicy couture outlet, http://www.juicycoutureoutlet.net/
calvin klein underwear, http://www.calvinklein.in.net/
ray ban sunglasses, http://www.raybansunglass.us.com/
prada outlet, http://www.pradaoutlet.us/
lululemon outlet, http://www.lululemonoutletstore.in.net/
tory burch shoes, http://www.toryburchshoesoutlet.com/
michael kors outlet, http://www.michaelkorsoutlet.org.uk/
replica watches, http://www.replicawatchesforsale.us.com/
pandora jewelry, http://www.pandorajewelryoutlet.us.com/
nfl jerseys wholesale, http://www.nfljerseys-wholesale.us.com/
michael kors outlet, http://www.michaelkorsoutletonlinstore.us.com/
oakley outlet, http://www.oakleyoutlet.in.net/
oakley sunglasses, http://www.oakleysunglasses-outlet.us.com/
michael kors bags, http://www.michaelkorsbags.uk/
ray ban sunglasses, http://www.ray-bansunglasses.eu.com/
oakley sunglasses, http://www.oakley.in.net/
air max 2015, http://www.airmax2015.in.net/
the north face uk, http://www.thenorthfaces.org.uk/
swarovski outlet, http://www.swarovski-outlet.co.uk/
pandora jewelry, http://www.pandora.eu.com/
chanel handbags, http://www.chanelhandbags-outlet.co.uk/
1002maoqiuyun

chenlina said...

chenlina20151127
tory burch outlet
ugg boots
ugg boots
ugg outlet
ralph lauren outlet
louis vuitton handbags
coach outlet store online
ray ban sunglasses
coach factory outlet online
abercrombie kids
louis vuitton
ugg boots outlet
ray ban sunglasses
oakley sunglasses
polo ralph lauren
michael kors handbags
instyler max
coach outlet
the north face uk
ugg boots for women
air max 90
coach outlet online
true religion jeans outlet
cheap uggs
ugg sale
timberland outlet
uggs sale
uggs outlet
michael kors outlet
cheap toms
cheap uggs
ugg boots
louis vuitton outlet
louis vuitton
toms outlet
louis vuitton
prada handbags
abercrombie
ray ban sunglasses outlet
ralph lauren outlet
as

oakleyses said...

air jordan pas cher, ray ban uk, nike roshe, true religion jeans, air force, abercrombie and fitch, north face, new balance pas cher, sac longchamp, longchamp pas cher, mulberry, oakley pas cher, nike free run uk, coach outlet, nike blazer, hermes, michael kors, nike air max, air max, hollister pas cher, true religion jeans, ralph lauren pas cher, vans pas cher, michael kors, coach outlet, true religion jeans, hollister, michael kors, north face, nike air max, nike roshe run, coach purses, burberry, lululemon, michael kors, lacoste pas cher, hogan, tn pas cher, sac guess, replica handbags, louboutin pas cher, vanessa bruno, timberland, kate spade handbags, converse pas cher, ralph lauren uk, nike free

oakleyses said...

moncler, moncler outlet, marc jacobs, moncler, hollister, montre pas cher, vans, ray ban, replica watches, pandora charms, louis vuitton, supra shoes, sac louis vuitton pas cher, pandora jewelry, swarovski, pandora charms, moncler, bottes ugg, barbour, canada goose, ugg,uggs,uggs canada, louis vuitton, doke gabbana outlet, converse, louis vuitton, lancel, louis vuitton, hollister, moncler, pandora jewelry, doudoune canada goose, juicy couture outlet, nike air max, converse outlet, barbour jackets, moncler, ugg,ugg australia,ugg italia, links of london, moncler, gucci, canada goose outlet, canada goose, canada goose outlet, coach outlet, canada goose uk, ugg boots uk, canada goose, juicy couture outlet, ugg pas cher, wedding dresses, canada goose

chenlina said...

chenlina20160127
canada goose jackets
cheap ugg boots
ugg boots
christian louboutin outlet
cheap toms
coach outlet
oakley sunglasses
ugg boots
toms outlet
abercrombie and fitch new york
vans shoes sale
louis vuitton outlet
coach factory outlet
ray ban sunglasses
longchamp outlet
fitflops sale clearance
coach factory outlet
michael kors bag
hollister
fake oakley sunglasses
tod's shoes
cheap jerseys
louis vuitton
gucci outlet
tory burch outlet
cheap air jordans
ugg boots
supra shoes
louis vuitton
oakley vault
cheap nfl jerseys
louis vuitton outlet
nike trainers uk
michael kors handbags
lebron 11
louis vuitton outlet
cheap oakley sunglasses
uggs outlet
ugg sale
polo outlet
as

Gaia1956 said...

sectional sofas men suits lawyer http://www.movershoustontx.net moving to same day dental implants
movers in attorneys personal trainers cleaning suede
breast cancer

vanahi688 said...


Thuốc shisha
bình shisha giá rẻ
ke toan ha noi
hoc tin hoc van phong
dich vu ke toan
công ty kê toán hà nội
tieng anh newlight
dia chi hoc ke toan
dich vu ke toan gia re
trung tam ke toan ha noi
ke toan bac ninh
hoc chung chi ke toan
cong ty ke toan ha noi
kế toán hà nội
đào tạo kế toán tphcm
đào tạo kế toán tổng hợp
dạy kế toán thuế
đào tạo kế toán
trung tâm đào tạo kế toán hà nội
kế toán hà nội kthn
cong ty dich vu ke toan ha noi
day hoc ke toan
hoc ke toan thue
học kế toán thuế
trung tâm kế toán
đào tạo kế toán tốt nhất
làm báo cáo tài chính cuối năm
dịch vụ kế toán

xumeiqing said...

20160923meiqing
louis vuitton outlet
uggs
coach factory outlet
ed hardy
louboutin shoes
ray ban sunglasses
red bottoms shoes
coach factory outlet
polo ralph lauren outlet
ugg outlet

Unknown said...

cheap nba jerseys
ray bans
coach factory outlet
true religion outlet
nike huarache
cheap oakley sunglasses
fitflops
oakley sunglasses
instyler
coach outlet
20170114caihuali

Unknown said...

oakley sunglasses
abercrombie and fitch
ed hardy clothing
cleveland cavaliers
baltimore ravens jerseys
cheap basketball shoes
ecco shoes
nike huarache trainers
michael kors handbags
nike blazer

Unknown said...

snapbacks wholesale
gucci outlet online
coach outlet online
cheap oakley sunglasses
michael kors outlet online
ralph lauren outlet
michael kors outlet clearance
pandora jewellery
cheap ray ban sunglasses
michael kors outlet clearance
20170617lck

Unknown said...

ralph lauren polo
ugg outlet
cheap nfl jerseys
michael kors handbags outlet
nike shoes
jacksonville jaguars jersey
ferragamo outlet
rolex replica watches
polo ralph lauren
nike blazer

Digital Marketing Agency said...

Quikcbooks Support Phone Number

Quikcbooks Support Number
Quikcbooks Support Phone Number
Quikcbooks Tech Support Number
Quikcbooks Technical Support Number
Quikcbooks Tech Support Phone Number
Quikcbooks Technical Support Phone Number
Quikcbooks Helpdesk Phone Number
Quikcbooks Customer Service Phone Number
Quikcbooks Customer Service Number
Quikcbooks Customer Support Number

Quicken Support Phone Number

Quicken Support Number
Quicken Support Phone Number
Quicken Tech Support Number
Quicken Technical Support Number
Quicken Tech Support Phone Number
Quicken Technical Support Phone Number
Quicken Helpdesk Phone Number
Quicken Customer Service Phone Number
Quicken Customer Service Number
Quicken Customer Support Number

Sage Support Phone Number

Sage Support Number
Sage Support Phone Number
SageTech Support Number
Sage Technical Support Number
Sage Tech Support Phone Number
Sage Technical Support Phone Number
Sage Helpdesk Phone Number
Sage Customer Service Phone Number
Sage Customer Service Number
Sage Customer Support Number

payday loans said...

Most states have a greatest loaning sum for little credits, for example, Alabama where it is $500, and furthermore a most extreme charge for the advance,

cash advance said...

Further, instead of holding up days or half a month for a choice, numerous online organizations will react inside a few hours. Rivalry in this market is savage, so they will do what it takes to get your business.

Vanwingerden Urick said...

There are many spots where you can discover a payday advance. While some back organizations offer these, the most well-known place to get a payday credit from is a business that really has practical experience in fast money advances like these. Check Cashing Corona

Vanwingerden Urick said...

A title advance gives the additional cash you have to send your children to camp, and in addition for different costs, with no credit check and no extravagant loan fees.Auto Title Loans

Unknown said...

20180412xiaoke
ugg boots
kobe 9 elite
oakley sunglasses
adidas outlet
cheap nhl jerseys
oakley sunglasses sale
michael kors outlet
polo ralph lauren
prada shoes for men
nike shoes

zzyytt said...

lebron 15
hogan outlet
lebron soldier 11
moncler
asics gel kayano
adidas nmd
adidas flux
christian louboutin shoes
michael kors
air max 90

Unknown said...

nike cortez women
nike air force 1 low
birkin bag
michael kors outlet
timberland outlet
hogan outlet online
nike mercurial vapor
kayno
adidas ultra boost 3.0
adidas yeezy

Unknown said...

qzz0710
ray ban sunglasses
air jordan 3
stuart weitzman shoes
tory burch outlet
fitflops sale
kd 10 elite
bulls jerseys
ralph lauren polo shirts
ugg outlets
burberry outlet

5689 said...

zzzzz2018.11.6
north face
kate spade handbags
canada goose outlet
tory burch outlet
mbt shoes
red bottom shoes
ecco shoes
manolo blahnik
canada goose jackets
hogan shoes

Ada Smith said...

All good. I am a newcomer, so I jerked until the money was withdrawn. I waited 2 days on Yandex. Thanks to the admins and support service for listening to my whining on the forum and in the chat. Play people! Do not be greedy. All the rules with this casino! perfect online casino play for real money I often spend my time here