размещение формы с использованием curl в php - PullRequest
0 голосов
/ 13 мая 2011

Привет я пытаюсь опубликовать curl, но я не могу это сделать

это то, что я пробовал в Csharp, и он работает, но версия php не работает

C #

  WebRequest request = WebRequest.Create("http://www.somesite.com/somepage.php");
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            string postString = "email=email@email.com&dueday=1&duemonth=2&dueyear=3&Submit=Submit";
            ASCIIEncoding ascii = new ASCIIEncoding(); 
            byte[] postBytes = ascii.GetBytes(postString.ToString());
            request.ContentLength = postBytes.Length;

            Stream postStream = request.GetRequestStream(); 
            postStream.Write(postBytes, 0, postBytes.Length); 
            postStream.Close(); 
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;

Php

<?php

if (isset($_POST['email']) && trim($_POST['email']) != "") {
    //filter out everything but the needed information
    $cleanquery = array();
    foreach ($_POST as $key=>$value) {
        //newsletter name
        if (stripos($value, 'something') !== false) {
            $cleanquery[$key] = $value;
        }
        if ($key == 'dueday' || $key == 'duemonth' || $key == 'dueyear' || $key == 'email') {
            $cleanquery[$key] = $value;
        }
    }
    $queryline = "";
    $i=0;
    foreach ($cleanquery as $key=>$value) {
        if ($i == 0) {
            $queryline .= $key . "=" . $value;
        } else {
            $queryline .= '&amp;' . $key . '=' . $value;
        }
        $i++;
    }
    $url = 'http://www.somesite.com/somepage.php';
    $ch = curl_init();
       curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_POST,4);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $queryline);
    curl_exec($ch);
    $info = curl_getinfo($ch);
    curl_close($ch);
    echo $info['http_code'];
}
?>

Ответы [ 2 ]

2 голосов
/ 13 мая 2011
$queryline = "";
$i=0;
foreach ($cleanquery as $key=>$value) {
    if ($i == 0) {
        $queryline .= $key . "=" . $value;
    } else {
        $queryline .= '&amp;' . $key . '=' . $value;
    }
    $i++;
}

Вам не нужно этого делать, потому что CURLOPT_POSTFIELDS может или может быть настроен "в виде массива с именем поля в качестве ключа и данными поля в качестве значения" .

curl_setopt($ch, CURLOPT_POST,4);

Не уверен, почему у вас 4 в конце.

0 голосов
/ 14 мая 2011

http://www.html -form-guide.com / php-form / php-form-submit.html

нашел, как это сделать здесь

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...