Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过CURL-PHP的httppost_Php_Curl - Fatal编程技术网

通过CURL-PHP的httppost

通过CURL-PHP的httppost,php,curl,Php,Curl,我正在尝试用PHP文件中的CURL发送HTTP数据,代码没有发送数据 $url= 'http://www.example.com/test.php?' ; $msg= 'p1=1234&p2=1234&p3=1234' ; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL,$url); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl

我正在尝试用PHP文件中的CURL发送HTTP数据,代码没有发送数据

$url= 'http://www.example.com/test.php?' ;
$msg= 'p1=1234&p2=1234&p3=1234' ;

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL,$url);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $msg);

    curl_exec ($curl);
    curl_close ($curl);

您需要将post字段作为数组而不是“GET”请求字符串发送:

$msg = [
 'p1' => '1234',
 'p2' => '1234',
 'p3' => '1234',
];
如果要将其作为一条消息发送,可以发送一个具有一个值的数组:

$msg = ['msg' => 'some_message'];
如果要发送GET请求,请将查询字符串附加到URL:

curl_setopt($curl, CURLOPT_URL, $url.$msg);

将来,您可以使用以下代码获取调试信息:

$result = curl_exec($curl);
echo 'curl_error: ';
var_dump(curl_error($curl));
echo '<br /><br /> curl_result: ';
var_dump($result);
curl_close($curl);
$result=curl\u exec($curl);
回显“卷曲错误:”;
变量转储(curl\u错误($curl));
回显“

curl_结果:”; var_dump($结果); curl_close($curl);
请尝试下面的代码好吗

$requestBody可以是数组、querystring、json或其他,具体取决于您的请求头和接收http请求的请求处理程序。

<?PHP
error_reporting();
ini_set('display_errors', 'On');

function makeRequest($url, $requestBody)
{
    $handle = curl_init();
    $headers = array(); //array of request headers
    //Example headers for standart browser request
    //$headers = array(
    //   'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8');

    curl_setopt($handle, CURLOPT_URL, $url);
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($handle, CURLOPT_POST, 1);
    curl_setopt($handle, CURLOPT_POSTFIELDS, $requestBody);
    curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($handle, CURLOPT_HEADER, 1); //This means include headers in response

    $result = curl_exec($handle);
    $header_size = curl_getinfo($handle, CURLINFO_HEADER_SIZE);
    $responseHeaders = substr($result, 0, $header_size);
    $responseBody = substr($result, $header_size, strlen($result) - $header_size);
    $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
    if ($httpCode == 200)
        return $responseBody;
    else
        throw new Exception($responseBody);
}

$method = $_SERVER["REQUEST_METHOD"];

if ($method == "GET") {

    $request = array();
    $request["a"] = "1";
    $request["b"] = "3";
    $request["c"] = "4";
    $request["d"] = "7";
    //$request = "a=1&b=3&c=4&d=7"; //This is the same with array version for standart post request.
    $response = makeRequest("http://localhost/test-curl.php", $request);
    echo $response;
} else {
    print_r($_POST);
}
?>
测试curl.php的内容:

<?PHP
error_reporting();
ini_set('display_errors', 'On');

function makeRequest($url, $requestBody)
{
    $handle = curl_init();
    $headers = array(); //array of request headers
    //Example headers for standart browser request
    //$headers = array(
    //   'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8');

    curl_setopt($handle, CURLOPT_URL, $url);
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($handle, CURLOPT_POST, 1);
    curl_setopt($handle, CURLOPT_POSTFIELDS, $requestBody);
    curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($handle, CURLOPT_HEADER, 1); //This means include headers in response

    $result = curl_exec($handle);
    $header_size = curl_getinfo($handle, CURLINFO_HEADER_SIZE);
    $responseHeaders = substr($result, 0, $header_size);
    $responseBody = substr($result, $header_size, strlen($result) - $header_size);
    $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
    if ($httpCode == 200)
        return $responseBody;
    else
        throw new Exception($responseBody);
}

$method = $_SERVER["REQUEST_METHOD"];

if ($method == "GET") {

    $request = array();
    $request["a"] = "1";
    $request["b"] = "3";
    $request["c"] = "4";
    $request["d"] = "7";
    //$request = "a=1&b=3&c=4&d=7"; //This is the same with array version for standart post request.
    $response = makeRequest("http://localhost/test-curl.php", $request);
    echo $response;
} else {
    print_r($_POST);
}
?>

如果使用url运行test-curl.php,它将向自身发出post请求,您将看到$\u post数组的打印输出。像下面这样

数组([a]=>1[b]=>3[c]=>4[d]=>7)


希望这对您有所帮助。

您会遇到什么错误?你期望什么样的产出?你怎么知道它没有发送数据?没有收到任何错误,只是字符串没有发送。有很多这样的问题。。。谢谢,您通过创建数组解决了其他PHP文件中的类似问题。努力工作。关于这里的情况,是否可以作为一条消息而不是数组发送?我将$requestBody重命名为$msg,以保持变量的多功能性,makerequest($url,$msg);Get:PHP致命错误:C:\Inetpub\www\example.com\httpdocs\test.PHP:84堆栈跟踪:#0 C:\Inetpub\www\example.com\httpdocs\test.PHP(59):makeRequest(‘?’,‘p1=1234&p2=1234&p3=1234’)#1{main}在第84行抛出inC Inetpub\www\example.com\httpdocs\test.PHP为了调试自己,我修改了我的答案。如果方法在makeRequest方法的末尾出现异常,则响应代码不是200,这表示OK。请检查我修改过的答案。请注意,您必须提供完整的url,而不是相对的url@user1570701cihanbey,我在测试时使用的是真实的域名。有一次,我写了完整的域名,一些黑客开始ping服务器,直到服务器崩溃。从那时起,我就害怕发布真实的域名。我可以私下给你发域名而不是公开的。我正在努力让它工作,我确信我遗漏了一些愚蠢的观点。海姆贝,没关系,你不需要在这里写下你的确切域名。但我修改后的答案应该会给你一个输出,甚至是一个错误或异常消息。我更新的答案的输出是什么@HaimRodrikI我在猜问题。在您的代码中有两个变量,$requestBody和$headers我的变量如下:$url='?'$消息='p1=1234&p2=1234&p3=1234';实际上不需要数组,因为$msg在一个变量中包含所有字段,而不需要转换为数组。您认为是否需要像您的示例中那样使用explode命令并插入到数组中?