JSON データを Symfony2 php サーバーに送信する必要がある Objective C プロジェクトがあります。
これはごく普通のことであり、Stackoverflow で多くのドキュメントと以前の問題を読みましたが、私のコードは正しいと思います。しかし、何らかの理由で、サーバーでデータを取得できません。
コードと結果の 2 つの異なるバージョンをお見せしましょう (どちらの例でも、NSMutableURLRequest *request
変数は適切に宣言されていると思われます。
すべての http 見出しを設定した後、次のようにします。
NSString *postString = [NSString stringWithFormat:@"hist_id=%d", 10];
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
サーバーに送信されると、完全な Request 値は次のようになります。
POST /~pgbonino/Symfony/web/app.php/api/apiGetQuestions HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Accept-Language: en-us
Connection: keep-alive
Content-Length: 10
Content-Type: application/x-www-form-urlencoded
Cookie: PHPSESSID=8915n9cj4ak8fjna4emvnrsov5
Host: 127.0.0.1
Surrogate-Capability: symfony2="ESI/1.0"
User-Agent: PreparaTest/1.0 CFNetwork/609.1.4 Darwin/12.4.0
X-Php-Ob-Level: 1
hist_id=10 [] [] // <-- TAKE A LOOK AT THE BODY CONTENT
そして、PHPで行うと、$hist_id = $this->getRequest()->get('hist_id')
適切に10が得られます。
これまでのところすべて完璧です!
しかし、もっと複雑なデータ セットをサーバーに送信する必要がある場合はどうすればよいでしょうか。たとえば、NSDictionary の値の中にいくつかの配列があります。この場合、JSON を使用したほうがよいでしょう。
これは、この 2 番目のケースのコードと結果です。この 2 番目の例では、辞書と配列を使用せず、同じ「hist_id」値を持つ単純な辞書のみを使用することに注意してください。したがって、両方の例を並列化できます。
// Create an array with a dictionary with a unique "hist_id" value set to 10
// (just like before, but using a dictionary and converting it to JSON.
NSDictionary *postDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[NSString stringWithFormat:@"%d", 10], @"hist_id",
nil];
// Convert it to json string
NSString *json = [postDictionary JSONRepresentation]; // This is perfectly performed!
// Convert to NSData the json with the dictionary
NSData *requestData = [NSData dataWithBytes:[json UTF8String] length:[json length]];
// Set headers and the body to the request
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: requestData];
サーバーに送信されると、残念ながら、サーバー側でリクエストを適切に取得できません。
POST /~pgbonino/Symfony/web/app.php/api/saveTest HTTP/1.1
Accept: application/json
Accept-Encoding: gzip, deflate
Accept-Language: en-us
Connection: keep-alive
Content-Length: 15
Content-Type: application/json
Cookie: PHPSESSID=caihkf98lh8injmdju512fvbc7
Host: 127.0.0.1
Surrogate-Capability: symfony2="ESI/1.0"
User-Agent: PreparaTest/1.0 CFNetwork/609.1.4 Darwin/12.4.0
X-Php-Ob-Level: 1
{"hist_id":"10"} [] [] // <-- TAKE A LOOK AT THE BODY CONTENT AND COMPARE WITH THE ABOVE EXAMPLE!!
どうやら大丈夫そうですが、行ってみると$this->getRequest()->get('hist_id')
何の価値もありません。
コンテンツを取得するために私ができる唯一のことは次のとおりです。
$content = json_decode($request->getContent());
$hist_id = $content->{'hist_id'};
これは正しいアプローチですか?$this->getRequest()->get('hist_id');
JSON経由ですべてを送信するときに、「hist_id」値を取得する可能性はありませんか? わかりました。json_decode を使用できますが、アプリケーションがマルチチャネルであると想定されており、Web (JQuery.post) からデータを送信すると、コンテンツが自動的に php データ構造に変換されるため、それを使用したくありません。 . 「誰が」情報を求めているかによって、API に異なるコードを使用したくありません。