2013-03-14 129 views
0
發送授權密鑰

我是新來的狂飲,我試圖生成以下REST調用:PHP +狂飲,在頭

https://product-search.api.cj.com/v2/product-search?website-id=1594990&keywords=%2Bsony+-camera 

GET /v2/product-search?website-id=1594990&keywords=%2Bsony+-camera HTTP/1.1 
Host: link-search.api.cj.com 
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.8) Gecko/2009032609 Firefox/3.0.8 
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 
Accept-Language: en-us,en;q=0.5 
Accept-Encoding: gzip,deflate 
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 
Keep-Alive: 300 
Connection: keep-alive 
Cookie: __utma=128446558.2099392322683464700.1239639722.1239639722.1239927095.2; __utmz=128446558.1239639722.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); CONTID=8073; cjuMember=0; JSESSIONID=aM5RSWdqdd_5 
Authorization: YOUR DEV KEY HERE 

HTTP/1.x 200 OK 
Server: Resin/2.1.17 
Content-Type: application/xml 
Transfer-Encoding: chunked 
Date: Thu, 26 Apr 2009 10:25:03 GMT 

我使用下面的PHP代碼:

$client = new Client('https://linksearch.api.cj.com', array(
    'id' => $website_id, 
    'keywords' => 'sony', 
)); 
$request = $client->get("v2/link-search?website-id={id}&keywords={keywords}"); 
$request->addHeader('Authorization', $dev_key); 
$response = $request->send(); 

這裏的問題是,與addHeader()語句我得到的答案「壞請求」,沒有addHeader()我得到「未經授權」。這似乎是我沒有正確發送我的身份驗證信息。有誰知道我在這裏做錯了嗎?

+0

儘量避免在URI參數:{ID},{關鍵詞}。到目前爲止,我還沒有能夠正確地使用它們,並且出於無奈,我最終像這樣手動添加它們:$ request-> getQuery() - > add('query','123')。有關更多信息,請參閱guzzlephp.org/http-client/request.html – Onema 2013-09-17 16:10:52

+0

- 取決於版本,語法可能有所不同。我建議那些訪問這個網頁的人使用這裏的最新版本http://docs.guzzlephp.org/en/latest/index.html,如果它不起作用,不要氣餒。只要嚴格按照說明書操作,就像在我的情況下一樣guzzle6。 – Dung 2016-02-22 20:32:35

回答

1

實際這裏問題是:

1)頭「授權」可能是大小寫敏感的,可能需要小寫。

2)如果任何請求參數不正確,即使是您的網站ID,也會引發400錯誤。 400錯誤意味着「由於驗證以外的原因,請求無效」。

+1

有趣的是,頭部區分大小寫。我已經看到了其他頭文件區分大小寫的情況,並且所有這些都忽略了HTTP/1.1不應該關心頭文件的情況。然而,這就是爲什麼Guzzle會尊重您提供的任何情況,而不是強制特定的套管。 – 2013-03-19 02:07:32

0

我想你正在創建$請求不正確。試試這個:

$client = new Client(); 
$request = new Request('POST', 'http://someuri/path/here'); 

然後使用$客戶端發送請求:

$response = $client->send($request);