我刚刚创建了这个PHP脚本,运行良好。
更新
(删除了很多与此问题无关的文字。)
唯一的事情:我不知道它为什么会起作用。 (返回$ result,我不知道如何)
更新
由于此处的请求,原始(合理的信息替换)脚本有问题:
<?php
$http_origin = $_SERVER['HTTP_ORIGIN'];
if ($http_origin == "https://some.domains.tld" ||
$http_origin == "https://some.domains.tld" ||
$http_origin == "https://some.domains.tld" ||
$http_origin == "https://some.domains.tld" ||
$http_origin == "https://some.domains.tld")
{
header("Access-Control-Allow-Origin: ".$http_origin);
}
### global variables
$curl; $mode = $_POST['mode'];
### init logging
$file = fopen('error.txt', 'w');
### log request source
#fwrite($file, "request from: ".$http_origin."\n");
### create variable parameters
$api_key='mysecretkey';
###Init curl
#fwrite($file, "Ok, try to include curl..."."\n");
try{
$curl = curl_init();
}catch(Exception $e){
fwrite($file, "curl init failed. Look: "."\n".$e."\n");
return null;
}
switch ($mode) {
case 'alldatas':
alldatas();
break;
case 'adata':
adata();
break;
case 'register':
register();
break;
default:
return;
}
###Get list of all datas
function alldatas()
{
global $curl;
global $api_key;
global $file;
$result;
$params = ['api_key' => $api_key];
try{
curl_setopt($curl, CURLOPT_URL, "https://some.third-party.service/api/v2/ever/webinars");
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
$result = curl_exec($curl);
}catch(Exception $e){
fwrite($file, "data list failed. Look: "."\n".$e."\n");
return null;
}
return $result;
}
###Get a data
function adata()
{
global $curl;
global $api_key;
global $file;
$result;
$data_id = $_POST['data_id'];
if($data_id){
$params = [
'api_key' => $api_key,
'data_id' => $data_id
];
try{
curl_setopt($curl, CURLOPT_URL, "https://some.third-party.service/api/v2/ever/webinar");
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
$result = curl_exec($curl);
}catch(Exception $e){
fwrite($file, "webinar fetch failed, man. Look: "."\n".$e."\n");
return null;
}
return $result;
}
}
###Register user to a datas
function register(){
global $curl;
global $api_key;
global $file;
$result;
$data_id = $_POST['data_id'];
$name = $_POST['name'];
$email = $_POST['email'];
$schedule = $_POST['schedule'];
$timezone = $_POST['timezone'];
if($data_id && $name && $email && $schedule && $timezone){
$params = [
'api_key'=>$api_key,
'data_id'=>$data_id,
'name'=>$name,
'email'=>$email,
'schedule'=>$schedule,
'timezone'=>$timezone
];
try{
curl_setopt($curl, CURLOPT_URL, "https://some.third-party.service/api/v2/ever/register");
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
$result = curl_exec($curl);
}catch(Exception $e){
fwrite($file, "data list failed. Look: "."\n".$e."\n");
return null;
}
}
return $result;
}
?>
答案 0 :(得分:4)
您未在curl_setopt()
中设置CURLOPT_RETURNTRANSFER
。这意味着当调用curl_exec()
时,cURL传输的结果是回显输出!返回数据的来源。
当CURLOPT_RETURNTRANSFER
为TRUE
时,curl_exec()
会返回数据,并且不会回显任何内容。但是,当CURLOPT_RETURNTRANSFER
为FALSE
(或未设置)时,curl_exec()
会回显数据并返回TRUE
。
答案 1 :(得分:1)
curl_exec 回声
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
答案 2 :(得分:1)
旧脚本使用curl_exec()
输出数据。
默认情况下curl_exec()
发送输出它收到的响应内容并返回操作成功。如果您希望它不将数据发送到输出但返回,则必须在curl_exec()
之前致电curl_setopt($curl, CURLOPT_RETURNTRANSFER)
。