首页 > 解决方案 > 添加 HTTP 表单后,Curl post 请求似乎不起作用

问题描述

我正在尝试使用 restful api 向我的 shadowsocks 服务器添加一个端口。

JS 代码获取站点的用户 id 并将其转换为 curl 代码

<script type="text/javascript">
function addUser() {
// Get user id
var div = document.getElementById("dom-target");
// Take user id text
var uid = div.textContent;
// Multiply by 1 to make sure JS thinks it's a number
var multiply = uid*1
// Generate password
var pass = Math.floor(Math.random() * 99999999999) + 10000000000 ;
// Generate port number
var port = 1024 + multiply; 
// Change format to fit cURL
var curl = '{"port": ' + port + ',"password":"' + pass + '"}';  
// Push to HTML
document.getElementById("curl").innerHTML = curl;
}
onload=addUser;

PHP 代码 - 当我只键入手动生成的代码 JS 脚本时,{"port": 1025,"password":"26909395438"}它就可以工作。每当我把它变成一种形式时,它就会停止工作。

<?php
$curl = $_REQUEST['curl'];
$data = array('curl'=>$curl);
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://[ip]:4001/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0;     Windows NT 5.1)");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

$headers = array();
$headers[] = "Content-Type: application/json";
$headers[] = "Authorization: Bearer     [token]";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

//perform our request
$result = curl_exec($ch);

//show information regarding the request
print_r(curl_getinfo($ch));
echo curl_errno($ch) . '-' .
            curl_error($ch);
?>

以及我发布 php 代码的 HTML 代码。

<p id="code"></p>
<form action="/create-beta.php" method="post">
<input type="hidden" name="curl" id="curl">
<br>

我是编码新手,我一直在互联网上进行挖掘以将其组合在一起。任何帮助将不胜感激。先感谢您。

标签: javascriptphprestapicurl

解决方案


推荐阅读