2011-02-10 4 views
3

업데이트 : 내가 긁고있는 사이트에서 필요한 데이터를 얻는 다른 방법을 제공하여 더 이상이 문제가 발생하지 않습니다. 그러나 저는 여전히 교육적인 이유로 솔루션에 관심이 있습니다.POST와 함께 cURL을 사용하여 JSON 서버로 데이터를 전달하는 방법


POST를 통해 JSON 서버에 데이터를 전달하려면 cURL을 사용하고 싶습니다.

cURL의 변수를 형식화하는 방법을 모르겠습니다.

이 (반환 NULL) 작동하지 않습니다

define('POSTVARS', 'json='.urlencode('{"cid":"2623","strQuery":"","strValues":"undefined","currentPage":"0","pageSize":"-1","pageSort":"-1","countryId":"2","maxResultCount":""}')); 

를도 않습니다이 내가

curl_setopt($ch, CURLOPT_HTTPHEADERS,array('Content-Type: application/json')); 

내 스크립트 작품 헤더를 설정하고 Post JSON using Curl 당으로

define('POSTVARS', "json=%7B'cid'%3A'2623'%2C%20'strQuery'%3A''%2C%20'strValues'%3A'undefined'%2C%20'currentPage'%3A'0'%2C%20'pageSize'%3A'-1'%2C'pageSort'%3A'-1'%2C'countryId'%3A'2'%2C'maxResultCount'%3A''%7D="); 

일반 POST 변수의 경우에는 JSON 서버에 데이터를 전달하려고 할 때가 아니라면 데이터를 잘못 형식화했거나 다른 추가 매개 변수를 제공해야합니다. 내 시도의

전체 코드 (포맷 종류의 내가 여기 https://docs.google.com/document/d/1hokE6-oMtcs3MBgPUzPwJvZIWNABc3XjOO2IzbpxDF4/edit?hl=en&authkey=CKXcnv8C를 전체 스크립트를 붙여, 아래에 분류됩니다) :

function get_url($url, $javascript_loop = 0, $timeout = 5) { $cookie = tempnam ("/tmp", "CURLCOOKIE"); $ch = curl_init(POSTURL); curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1"); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_ENCODING, ""); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_AUTOREFERER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); # required for https urls curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); curl_setopt($ch, CURLOPT_MAXREDIRS, 10);

$postvars=<<<HEREDOC 
{'cid':'2623', 'strQuery':"", 'strValues':'undefined', 'currentPage':'0', 'pageSize':'-1','pageSort':'-1','countryId':'2','maxResultCount':''} 

히어 닥;

curl_setopt($ch, CURLOPT_POST  ,1); 
curl_setopt($ch, CURLOPT_POSTFIELDS ,$postvars); 
curl_setopt($ch, CURLOPT_HEADER  ,0); // DO NOT RETURN HTTP HEADERS 

$arr = array(); 
array_push($arr, 'Content-Type: application/json; charset=utf-8'); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $arr); 

$content = curl_exec($ch); 
$response = curl_getinfo($ch); 
curl_close ($ch); 

if ($response['http_code'] == 301 || $response['http_code'] == 302) 
{ 
    ini_set("user_agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1"); 

    if ($headers = get_headers($response['url'])) 
    { 
     foreach($headers as $value) 
     { 
      if (substr(strtolower($value), 0, 9) == "location:") 
       return get_url(trim(substr($value, 9, strlen($value)))); 
     } 
    } 
} 

if ( (preg_match("/>[[:space:]]+window\.location\.replace\('(.*)'\)/i", $content, $value) || preg_match("/>[[:space:]]+window\.location\=\"(.*)\"/i", $content, $value)) && 
     $javascript_loop < 5 
) 
{ 
    return get_url($value[1], $javascript_loop+1); 
} 
else 
{ 
    return array($content, $response); 
} 

}

// 설정 URL $의 URL = "http://us.asos.com/services/srvWebCategory.asmx/GetWebCategories";

$ output = get_url ($ url);

print_r ($ output); 대신 CURLOPT_HTTPHEADERS의

+0

CURLOPT_POSTFIELDS와 (과) 함께 curl_setopt을 (를) 호출 하시겠습니까? – Dereleased

+0

@ceejayoz 그렇습니다. 데이터를 querystring 형식 (요르단의 응답)으로 변환하겠다는 생각이 들었지 만 성공하지 못 했으므로 내가 잘못하고있는 다른 것이 있어야합니다. @Dereleased 예, 내 스크립트는 JSON 서버가 아닌 웹 양식으로 게시물을 통해 데이터를 전송하기 위해 작동하므로 내 문제는 변수를 형식화하는 방법과 관련되거나 JSON에 게시하는 것과 관련됩니다. 섬기는 사람. – jela

+0

데이터에 json_encode를 사용해 보셨습니까? – DeaconDesperado

답변

관련 문제