php - 使用 PHP 发布并获取结果

标签 php post libcurl

从一个 PHP 页面,我试图将一些数据发布到另一个 PHP 页面并取回数据。

这是我目前拥有的:

<?php 
// Initialize cURL session
$ch = curl_init('postpage.php');

// Set some options on the session
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('field1' => 'Andrew'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Execute post
$result = curl_exec($ch);

var_dump($result);

// Close the session
curl_close($ch);
?>

在 postpage.php 中:

<?php
echo 'Receipt of post request. field1:'.$_POST["field1"];
?>

所有 var_dump 给我的是:

string(0) "" 

为什么我收不到短信?

谢谢!

最佳答案

从您的 php 脚本执行的 curl 不知道当前环境,也就是说,您不能使用相对 url。您提供给 curl_init 的 url 必须是绝对的(即包括 http://)

关于php - 使用 PHP 发布并获取结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9518930/

相关文章:

php - 处理 POST 请求

php - 如何从用户获取多行输入并在 PHP 中显示相同内容

php - Composer/Packagist 忽略分支

PHP函数在页面加载时自动调用,而不是通过onclick事件调用

php - stdClass 类的对象无法转换为字符串 - 找不到解决方案

php - 需要在有 cPanel 的服务器中安装 mysql-devel - `libmysqlclient_16' 未找到

javascript - 使用 gmail api 发送电子邮件错误 : This API does not support parsing form-encoded input

C++ libcurl http响应代码问题

c++ - 在 64 位 OSX 上构建 32 位 libcurl

libCurl : curl_easy_setopt in one method and curl_easy_perform in another does not work