简体   繁体   English

如何使用 curl 将数据服务器发送到另一台服务器?

[英]how to send data server to another server using curl?

To be clear, I want the page containing my form (page1.php) to check the data entered by the user from my other server page ( https://anotherserver.com/checker ) and send it to page1.php as true or false.明确地说,我希望包含我的表单(page1.php)的页面检查用户从我的其他服务器页面( https://anotherserver.com/checker )输入的数据并将其发送到 page1.php 为真或错误的。 This is my first time using curl.这是我第一次使用 curl。 I can't see anything with this code..这段代码我看不到任何东西..

here is my form page code page1.php这是我的表单页面代码page1.php

function dataFn($url, $data = array()) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 3000);
    $data = curl_exec($curl);
    curl_close($curl);
    return json_decode($data, true);
}

$data = dataFn(base64_encode('my base64 key'), ['value1' => $value2, 'value2' => $value2]);

var_dump($data);
die();

if (!$data) {
    // some err
} else {
    if ($data['status'] == 0) {
        // some code
    }
}

here is my checker server page这是我的检查服务器页面

<?php 
// validation page
    print_r($_POST['value1']);
?>

You didn't make too many errors.你没有犯太多错误。 It definitely will not with your code.它绝对不会与您的代码一起使用。

Two ways to make your post data.制作帖子数据的两种方法。

$post = 'key1=value1&key2=value2&key3=value3';
$post = array('key1'=>value1,'key2'=>value2,'key3'=>'value3');

depending on the data you may need to use urlencode()根据您可能需要使用 urlencode() 的数据

$post = urlencode($post);

I do a lot of curl these are my standard post options我做了很多 curl 这些是我的标准帖子选项

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_ENCODING,"");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request);
curl_setopt($ch, CURLOPT_ENCODING,"");

these are my troubleshooting options这些是我的故障排除选项

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT,10);
curl_setopt($ch, CURLOPT_FAILONERROR,true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request);

You NEED to see the request (out) and response headers (in), so you NEED these these two options.您需要查看请求(输出)和响应标头(输入),因此您需要这两个选项。
These two option must come out after fixing the problem.这两个选项必须在解决问题后出现。

curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HEADER, true);

If it's HTTPS you need this one:如果是 HTTPS 你需要这个:

curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);

I always make my own request headers.我总是制作自己的请求标头。 You can remove that option, it's not mandatory.您可以删除该选项,这不是强制性的。

$request = array();
$request[] = "Host: www.example.com";
$request[] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
$request[] = "User-Agent: MOT-V9mm/00.62 UP.Browser/6.2.3.4.c.1.123 (GUI) MMP/2.0";
$request[] = "Accept-Language: en-US,en;q=0.5";
$request[] = "Connection: keep-alive";
$request[] = "Cache-Control: no-cache";
$request[] = "Pragma: no-cache";

If you are having trouble making the POST you look at the header you send and receive.如果您在 POST 时遇到问题,请查看您发送和接收的 header。

$data = curl_exec($ch);
if (curl_errno($ch)){echo 'Error: ' . curl_error($ch);}

Sometimes I will save the all response curl info as text and sometime echo有时我会将所有响应 curl 信息保存为文本,有时会回显

$info = rawurldecode(var_export(curl_getinfo($ch),true));
echo rawurldecode(var_export(curl_getinfo($ch),true));

You want the CURLINFO_HEADER_OUT from the curl_getinfo()你想要CURLINFO_HEADER_OUT curl_getinfo()中的 CURLINFO_HEADER_OUT
Then you can see what you really sent.然后你就可以看到你真正发送了什么。

echo curl_getinfo($ch,CURLINFO_HEADER_OUT);

The http response id very important to know. http 响应 ID 非常重要。

$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

There is a lot of info in the curl_getinfo which may give you other hints. curl_getinfo 中有很多信息可能会给您其他提示。

If I had the URL I'm sure I could get it working.如果我有 URL 我相信我可以让它工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM