Php “卷曲请求转换”;htmlentities“;在URL查询字符串中
我发送的URL包含一个名为Php “卷曲请求转换”;htmlentities“;在URL查询字符串中,php,curl,Php,Curl,我发送的URL包含一个名为timeslot\u start的查询变量: $URL='https://api.example.com/endpoint?timeslot_start=gte_1600749639'; 结果包含“下一个”url的参数(page=2添加到查询字符串) 当我在命令行上发出常规cURL请求时,url看起来像我期望的: https://api.example.com/endpoint?timeslot_start=gte_1600749639&page=2 但当我
timeslot\u start
的查询变量:
$URL='https://api.example.com/endpoint?timeslot_start=gte_1600749639';
结果包含“下一个”url的参数(page=2
添加到查询字符串)
当我在命令行上发出常规cURL请求时,url看起来像我期望的:
https://api.example.com/endpoint?timeslot_start=gte_1600749639&page=2
但当我用php编写时:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$URL);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
$result=curl_exec ($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //get status code
curl_close ($ch);
在这条线路的某个地方,×
正在转换为x
:
https://api.example.com/endpoint?xlot_start=gte_1600749639&page=2
如何防止这种情况发生?因此,只有在查看查询字符串时才发生转换 当我将API返回的URL(和查询字符串)打印到屏幕上时,
$timeslot\u start
被转换为xlot\u start
我花了相当长的时间在Wordpress代码库中挖掘:
1. `wp_post_request` calls
2. `class-http.php` methods
3. `class-request.php` methods
最后,上面有人在评论中帮助我意识到,我只是在HTML中查看数据,数据被破坏了
将数据写入文件:
/**
* Helper function to write strings or arrays to a file
*
* @since 1.0.1
*
* @param $message the content to be written to file.
* @param $file_path string optional path to write file to.
*/
public function log($message, $file_path='')
{
$file_path = ( ($file_path == '') || !file_exists($file_path) ) ? WP_CONTENT_DIR . '/mobilize_america.log' : $file_path;
$header = date('l dS \o\f F Y h:i:s A', strtotime("now")) . "\t ";
// Just keep up to seven days worth of data
if (file_exists($file_path)){
if (time() - filemtime($file_path) >= 60 * 60 * 24 * 7) { // 7 days
unlink($file_path);
}
}
if (is_array($message) || is_object($message)) {
$message = print_r($message, true);
}
$message .= "\n";
file_put_contents(
$file_path,
$header . $message,
FILE_APPEND | LOCK_EX
);
}
我意识到返回的URL实际上很好,在发送之前,我在其上运行了
htmlentities()
,从而弄乱了发送的查询字符串。不要将参数放在URL本身中,使用与-d name=value
(每个name=value对一个)等效的PHP我能想象发生这种情况的唯一环境是HTML。浏览器可能会将×
解释为乘法字符×
——尽管“实体”缺少一个开头的尾随分号。但在您给出的示例URL中,您甚至没有×
,您有?times
…到目前为止,这一点并不清楚。你到底是在什么时候遇到这个问题的?@04FS你是对的。只有当我将返回的URL打印到屏幕上时,HTML实体才会被转换。当我把它记录到一个文件中时,它看起来和预期的一样。非常感谢。