0

我正在尝试创建一个 php 脚本,它将一个 post 变量传递给一个在线表单并返回信息。我已经使用命令行和以下命令成功地做到了这一点:

curl --data "cid=800.10" http://online.starmont.ca/java/www?html=main

不幸的是,我似乎无法让它与 php 一起使用。如果有人有任何建议,将不胜感激。提前致谢。这是我目前拥有的 PHP 脚本。

<?php

//set up a connection variable for the page you will post data to
$curl_connection = curl_init('http://online.starmont.ca/java/www?html=main');

//curl basic setup
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, 1);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);

//$_POST variables to pass
$post_item = 'cid='.strip_tags($_POST['800.10']);

//format the $post_items into a string
$post_string = implode ('&', $post_items);

//send the $_POST data to the new page
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
$result = curl_exec($curl_connection);

print ($result);
curl_close($curl_connection);
?>
4

2 回答 2

0

确保您输入:

curl_setopt($curl_connection, CURLOPT_POST, true);

curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);

否则它似乎没有发送任何数据!(无论如何对我来说)

于 2014-03-01T05:52:03.253 回答
0

您还需要设置

curl_setopt($curl_connection, CURLOPT_POST, true);

为了让 curl 执行一个POST请求。

欲了解更多信息:http ://www.php.net/curl_setopt

于 2013-10-16T05:31:46.413 回答