0

我正在尝试用 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 的示例?

4

2 回答 2

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);
于 2012-06-06T18:20:27.000 回答
0

cURL主要用于从HTTP POST方法中检索呈现的 HTML。而且你做得非常好。现在你到底想检索什么?参数来自posturl.php? 然后最后您可以正确使用此代码:

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

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

于 2012-06-06T18:17:51.667 回答