2011-02-10 133 views
3

更新:私が掻いているサイトは、私が必要とするデータを得る別の方法を私に提供しています。しかし、私はまだ教育的な理由で解決策に興味があります。POSTでcURLを使用してJSONサーバーにデータを渡す方法


私は、cURLを使用してPOST経由でJSONサーバーにデータを渡したいと考えています。

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':''} 

ヒアドキュメント。 $ URL = "http://us.asos.com/services/srvWebCategory.asmx/GetWebCategories" URLを設定

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); 
} 

}

//;

$ output = get_url($ url);

print_r($ output);

+0

あなたはCURLOPT_POSTFIELDSでcurl_setoptを呼び出していますか? – Dereleased

+0

@ceejayozはい、データをクエルチング形式(ヨルダンの応答から)に変換するというアイデアがありましたが、成功していないので、私は間違っています。 @@Dereleasedはい、私のスクリプトはJSONサーバーではなく、Webフォームへの投稿経由でデータを送信するために機能するので、変数を書式設定する方法やJSONへの投稿に関連する問題が発生しているようですサーバ。 – jela

+0

あなたのデータにjson_encodeを試しましたか? – DeaconDesperado

答えて

関連する問題