2011-07-20 16 views
2

APIにデータを送信したい、データに単純な変数username、pasword、emailなどが含まれています これはPOSTメソッドを使用してデータを送信したいのですが、 Google上の問題は、すべてがCURLのために行くと言っている。フォームなしでPOSTメソッドを使用してデータを送信する

今、カールとは何ですか?それは関数、スクリプト、APIまたは何ですか?

他の方法はありますか?

私はこの

$username, $password to www.abc.com?username=$username&password=$password 

よろしく

答えて

2

cURLのは「あなたが接続し、プロトコルの多くの異なる種類のサーバの多くの異なる種類に通信できるようにする」ライブラリです。したがって、cURLを使用してHTTP POSTリクエストを送信できます。詳細については、PHPマニュアルをお読みください。http://www.php.net/manual/en/book.curl.php

しかし、cURLだけではありません。 PHP Streamsを使用することも、ソケット関数を使用してWebサーバーに接続してからリクエストを生成することもできます。

個人的に私はしばしばcURLを使用します。これは非常に柔軟性があり、簡単にクッキーを操作できるからです。 xmlファイルで設定

+0

実際に返信とともに投稿データを送信するためにcURLを使用する例は含まれていませんか? – n8bar

1

Php curl manualのようなものを送りたいが良い出発点です。続き

はStackOverflowの上のPHPのカールの使い方の例です。この機能を使用することにより

Send json post using php

3

あなたが オプションのパラメータの数とPOSTリクエストを送信することができます。ポストデータ配列にキー値のペアを入力するだけです。 使用例が提供されています。

<?php 

function do_post_request($url, $postdata) 
{ 
    $content = ""; 

    // Add post data to request. 
    foreach($postdata as $key => $value) 
    { 
    $content .= "{$key}={$value}&"; 
    } 

    $params = array('http' => array(
    'method' => 'POST', 
    'header' => 'Content-Type: application/x-www-form-urlencoded', 
    'content' => $content 
)); 

    $ctx = stream_context_create($params); 
    $fp = fopen($url, 'rb', false, $ctx); 

    if (!$fp) { 
    throw new Exception("Connection problem, {$php_errormsg}"); 
    } 

    $response = @stream_get_contents($fp); 
    if ($response === false) { 
    throw new Exception("Response error, {$php_errormsg}"); 
    } 

    return $response; 
} 

// Post variables. 
$postdata = array(
    'username' => 'value1', 
    'password' => 'value2', 
    'param3' => 'value3' 
); 

do_post_request("http://www.example.com", $postdata); 
?> 
+0

file_get_contents()は、ストリームを利用例・ソリューションを更新しました。 –

1

データ:

$post="<?xml version="1.0\" encoding=\"UTF-8\"?> 
<message xmlns=\"url\" > 
<username>".$username."</username> 
<password>".$password."</password> 

<status date=\"2010-11-05 14:44:10+02\"/> 
<body content-type=\"text/plain\">Noobligatory text</body> 
</message>"; 

$url = "a-url-to-send-the-data"; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,$url); 
curl_setopt($ch, CURLOPT_FAILONERROR, 1); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
curl_setopt($ch, CURLOPT_TIMEOUT, 6); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 

$result = curl_exec($ch); 
curl_close($ch); 
0

は単純に使用します。

// building array of variables 
$content = http_build_query(array(
      'username' => 'value', 
      'password' => 'value' 
      )); 
// creating the context change POST to GET if that is relevant 
$context = stream_context_create(array(
      'http' => array(
       'method' => 'POST', 
       'content' => $content,))); 

$result = file_get_contents('http://www.example.com', null, $context); 
//dumping the reuslt 
var_dump($result); 
関連する問題