使用curl发布和获取参数

时间:2012-06-06 18:14:09

标签: php curl

我试图用curl替换使用url(例如...... /?id = 123)的传递参数。如果不能使用curl则忽略。

我尝试在线查找示例,但我只能看到如何发布数据但没有得到它。我在网站上找到了这个,但我没有参考。

                        $post_data['ID'] = '1';

                        //traverse array and prepare data for posting (key1=value1)
                        foreach ( $post_data as $key => $value) {
                        $post_items[] = $key . '=' . $value;
                        }

                        //create the final string to be posted using implode()
                        $post_string = implode ('&', $post_items);

                        //create cURL connection
                        $curl_connection = curl_init('posturl.php');

                        //set options
                        curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
                        curl_setopt($curl_connection, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
                        curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
                        curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
                        curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);

                        //set data to be posted
                        curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);

                        //perform our request8
                        $curlresult = curl_exec($curl_connection);

                        //show information regarding the request
                        // print_r(curl_getinfo($curl_connection));
                        echo curl_getinfo($ch, CURLINFO_HTTP_CODE);

                        //close the connection
                        curl_close($curl_connection);           

如何从posturl.php获取参数

有没有人有一个发布和使用curl的例子?

2 个答案:

答案 0 :(得分:0)

cURL主要用于检索从HTTP POST方法渲染的HTML。你做得很好。那究竟你想要检索什么?来自posturl.php的参数?然后最后你可以正确使用这个代码:

foreach($post_items as $parameter => $value)
    echo "The value of $parameter is $value.\n";

我不确定我是否理解这个问题,但这是我可以帮助你的方式。希望这会有所帮助。

答案 1 :(得分:0)

尝试替换

//create cURL connection
$curl_connection = curl_init('posturl.php');

//create cURL connection
$curl_connection = curl_init("posturl.php?$post_string");

和评论

//curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);