通过 CURL PUT 传递 PHP 数组生成通知未定义索引
Passing PHP array through CURL PUT generates notice undefined index
我正在 PHP 开发一个基本的 REST 应用程序,我有以下代码。
客户端代码:
$ch = curl_init();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch , CURLOPT_HEADER,false);
curl_setopt($ch , CURLOPT_FOLLOWLOCATION , true);
curl_setopt($ch , CURLOPT_URL , "http://localhost/Hello/Rest");
curl_setopt($ch , CURLOPT_POSTFIELDS,
http_build_query(array("username" => "test")));
$output = curl_exec($ch);
print_r($output);
curl_close($ch);
服务器端代码:
$username = $_REQUEST['username'];
echo "This is a PUT request";
echo "<br>";
echo $username;
echo "<br>";
由于某些原因 $_REQUEST['username'] 不是 recognized.Generates 未定义的索引。
不太确定是什么导致了这个错误。
你必须return你的$output
$ch = curl_init();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch , CURLOPT_HEADER,false);
curl_setopt($ch , CURLOPT_FOLLOWLOCATION , true);
curl_setopt($ch , CURLOPT_URL , "http://localhost/Hello/Rest");
curl_setopt($ch , CURLOPT_POSTFIELDS,
http_build_query(array("username" => "test")));
$output = curl_exec($ch);
curl_close ($ch);
return $output;
正如@NaijaProgrammer 所指出的,$_REQUEST 不包含 PUT 值。如果您想坚持使用 PUT,则需要修改您的服务器代码。看到这个linkfor more information.
// for put requests
parse_str(file_get_contents("php://input"),$post_vars);
$username = $post_vars['username'];
echo "This is a PUT request";
echo "<br>";
echo $username;
echo "<br>";
我正在 PHP 开发一个基本的 REST 应用程序,我有以下代码。 客户端代码:
$ch = curl_init();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch , CURLOPT_HEADER,false);
curl_setopt($ch , CURLOPT_FOLLOWLOCATION , true);
curl_setopt($ch , CURLOPT_URL , "http://localhost/Hello/Rest");
curl_setopt($ch , CURLOPT_POSTFIELDS,
http_build_query(array("username" => "test")));
$output = curl_exec($ch);
print_r($output);
curl_close($ch);
服务器端代码:
$username = $_REQUEST['username'];
echo "This is a PUT request";
echo "<br>";
echo $username;
echo "<br>";
由于某些原因 $_REQUEST['username'] 不是 recognized.Generates 未定义的索引。
不太确定是什么导致了这个错误。
你必须return你的$output
$ch = curl_init();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch , CURLOPT_HEADER,false);
curl_setopt($ch , CURLOPT_FOLLOWLOCATION , true);
curl_setopt($ch , CURLOPT_URL , "http://localhost/Hello/Rest");
curl_setopt($ch , CURLOPT_POSTFIELDS,
http_build_query(array("username" => "test")));
$output = curl_exec($ch);
curl_close ($ch);
return $output;
正如@NaijaProgrammer 所指出的,$_REQUEST 不包含 PUT 值。如果您想坚持使用 PUT,则需要修改您的服务器代码。看到这个linkfor more information.
// for put requests
parse_str(file_get_contents("php://input"),$post_vars);
$username = $post_vars['username'];
echo "This is a PUT request";
echo "<br>";
echo $username;
echo "<br>";