2015-04-22 94 views
2

我想使用jobcrank提要API,它需要在頭 要發送的數據我想在下面的格式捲曲PHP頭數據

POST /v2/QueryService.asmx/QueryJob HTTP/1.1 
Host: api.jobcrank.com 
Content-Type: application/x-www-form-urlencoded 
Content-Length: length 

和查詢參數請求將在以下發送方法

apiKey=string&apiSerial=string&serviceTypeID=string&jobType=string&keyWordsType=string&keyWords=string&stateAbbreviation=string&county=string&city=string&isWorkFromHome=string 

我已經寫代碼也爲這一點,但不知道如何發送包含 「POST /v2/QueryService.asmx/QueryJob HTTP/1.1」行 這裏頭我的代碼:`

<?php 
     $fields = array(
    'apiKey'=>'XXX', 
    'apiSerial'=>'XXXX',); 
$fields_string=""; 
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; } 
rtrim($fields_string,'&'); 
$ch = curl_init(); 
curl_setopt($ch,CURLOPT_URL,'/v2/QueryService.asmx/QueryJob'); 
curl_setopt($ch,CURLOPT_POST,count($fields)); 
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string); 
$result = curl_exec($ch); 
print $result; 
?> 

回答

1

CURLOPT_URL需要與方案和主機的絕對URL:

curl_setopt($ch, CURLOPT_URL,'http://example.org/v2/QueryService.asmx/QueryJob'); 
+0

多麼美妙的回答感謝的人的包。它創造了我的一天。 –

0

你並不需要設置這個頭 - 它被自動完成。你需要做的是對你的$fields_string進行urlencode編碼,因爲它可能包含禁止使用的字符,如空格。要做到這一點,使用http_build_query直接從您的陣列構建此字符串:

$fields_string = http_build_query($fields); 

它將URLEncode的價值觀,使有效的後弦。

0

您不必明確添加POST標題。 Curl會爲你做到這一點,只需設置CURLOPT_POST標誌爲true,就是這樣。

curl_setopt ($ch, CURLOPT_POST, TRUE); 

看一看這個網站有疑問時:http://php.net/curl_setopt