簡體   English   中英

使用PHP cURL發送非鍵值對POST請求?

[英]Using PHP cURL to send a non key-value pair POST request?

使用RESTful API(用於Rackspace的域記錄API),我第一次遇到了問題。 也就是說,以JSON文件而不是通常的鍵值配對數據的形式發送請求。

就我而言,我應該以JSON格式發送字符串,而沒有與之對應的密鑰。 我在cURL中遇到的所有示例始終假定請求頁面期望一個。 此外, CURLOPT_POSTFIELDS選項似乎仍然需要一個數組(讀取:鍵值對)。

我能夠設置必要的標頭(Content-Type和其他身份驗證標頭),但是我嚴重地堅持將必要的JSON字符串放入請求中。

我怎樣才能做到這一點?

編輯:

以下是API文檔:

POST https://dns.api.rackspacecloud.com/v1.0/1234/domains/2725233/records
Accept: application/json
X-Auth-Token: ea85e6ac-baff-4a6c-bf43-848020ea3812
Content-Type: application/json
Content-Length: 725

{
  "records" : [ {
    "name" : "ftp.example.com",
"type" : "A",
"data" : "192.0.2.8",
"ttl" : 5771
  }, {
"name" : "example.com",
"type" : "A",
"data" : "192.0.2.17",
"ttl" : 86400
  }, {
"name" : "example.com",
"type" : "NS",
"data" : "dns1.stabletransit.com",
"ttl" : 3600
  }, {
"name" : "example.com",
"type" : "NS",
"data" : "dns2.stabletransit.com",
"ttl" : 3600
  }, {
"name" : "example.com",
"priority" : 5,
"type" : "MX",
"data" : "mail.example.com",
"ttl" : 3600
  }, {
"name" : "www.example.com",
"type" : "CNAME",
"comment" : "This is a comment on the CNAME record",
"data" : "example.com",
"ttl" : 5400
  } ]
}

JSON格式打算具有key:value格式。 另一方面,HTTP GET / POST協議也可以鍵/值格式使用。 應該指定參數名稱,以便偵聽器可以通過鍵獲取值來准備值。

假設您要發布文件:

$fh = fopen('/path/to/file', 'r');
$ch = curl_init($url);
curl_setopt_array($ch, array(
  CURLOPT_INFILE => $fh,
  CURLOPT_POST => TRUE,
  //...any other options you might need
));
curl_exec($ch);
curl_close($ch);

如果發布數據(如json),請使用CURLOPT_POSTFIELDS

$data = '{ "foo": "bar" }';
curl_setopt_array($ch, array(
  CURLOPT_POSTFIELDS => $data,
  CURLOPT_POST => TRUE,
  //...any other options you might need
));

我寫了一個類來做這個……基本上只是一個卷發包裝。 但是我將其清理干凈,因此可以輕松更改標頭,請求參數,請求正文等。

以下是一些有關如何入門的示例代碼:

https://github.com/homer6/altumo/blob/master/source/php/Http/OutgoingHttpRequest.md

//prepare the message body
    $record = new \stdClass();
    $record->name = 'ftp.example.com';
    $record->type = 'A';
    $record->data = '192.0.2.8';
    $record->ttl = 5771;

    $message_body = new \stdClass();
    $message_body->records = array(
        $record
    );

    $json_string = json_encode( $message_body );

//send the request and get the response
    $client = new \Altumo\Http\OutgoingHttpRequest( 'https://dns.api.rackspacecloud.com/v1.0/1234/domains/2725233/records' );
    $client->setRequestMethod( \Altumo\Http\OutgoingHttpRequest::HTTP_METHOD_POST );
    $client->addHeader( 'Accept', 'application/json' );
    $client->addHeader( 'X-Auth-Token', 'ea85e6ac-baff-4a6c-bf43-848020ea3812' );
    $client->addHeader( 'Content-Type', 'application/json' );
    $client->setMessageBody( $json_string );
    $response_string = $client->send();

//do something with the response
    $response = json_decode( $response_string );
    if( $response === false ){
        throw new \Exception( 'Invalid JSON response.' );
    }
    $value = $response->one; //or whatever the response values are

請注意,OutgoingHttpRequest方法可能會引發異常,因此您可能希望將其包裝在try / catch中。 我已嘗試將其記錄在案,但是如果我錯過了任何事情,請告訴我。 如果有不清楚的地方,我也可以提問。

我認為Zend構建了一個類似的HTTP包裝器,如果這更符合您的風格:

http://framework.zend.com/manual/en/zend.http.html

希望有幫助...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM