2015-10-07 37 views
1

这个PHP允许用户使用二进制或纯文本的URL提交表单。该URL然后转换为纯文本,我使用cURL加载响应,然后将其转换回二进制。如果你想知道,它来自二进制翻译器,它可以翻译文本 - >二进制和二进制 - >文本,但也接受URL。cURL没有设置URL

问题:您可以看到二进制URL被转换为文本,然后传递给cURL。明文值存储在$newtext中。我可以确认binaryToText()的确如我所做的一些调试所宣传的那样工作。纯文本URL(请参阅if语句的else部分)已成功设置,但转换后的二进制文件不是。

实例

例如$text = "http://google.co.uk";isBinary($text) == false

curl_setopt($ch, CURLOPT_URL, $text); < - 这适用

例如$text = "01101000011101000111010001110000001110100010111100101111011001110110111101101111011001110110110001100101001011100110001101101111001011100111010101101011"(同谷歌URL) (isBinary($text) == true

$newtext = binaryToText($text); curl_setopt($ch, CURLOPT_URL, $newtext);

echo curl_error($ch); - >输出 “没有URL集!”

某处存在这个问题......虽然我看不到它。

代码

$text = $_POST["text"]; 
if(startsWith($text, "http://") || startsWith($text, "https://") || startsWith($text, textToBinary("http://")) || startsWith($text, textToBinary("https://"))) { 
    // URL - accepts binary (prefered), or plain text url; returns binary. 
    $ch = curl_init(); 
    if(isBinary($text)) { 
    $newtext = binaryToText($text); 
    curl_setopt($ch, CURLOPT_URL, $newtext); 
    } else { 
    curl_setopt($ch, CURLOPT_URL, $text); 
    } 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_USERAGENT, "MY USERAGENT"); 
    if(isset($_GET["r"])) curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
    $result=curl_exec($ch); 
    echo curl_error($ch); 
    curl_close($ch); 
    echo textToBinary($result); 
} else if... 

功能

function isBinary($input) { 
    return !preg_match('/[^(0|1)]/', $input); // contains nothing but 0 and 1 
} 
function binaryToText($input) { 
    $return = ''; 
    $chars = explode("\n", chunk_split(str_replace("\n", '', $input), 8)); 
    $_I = count($chars); 
    for($i = 0; $i < $_I; $return .= chr(bindec($chars[$i])), $i++); 
    return $return; 
} 

编辑:中$newtext 输出包含在此输出:

text: 01101000011101000111010001110000001110100010111100101111011001110110111101101111011001110110110001100101001011100110001101101111001011100111010101101011 
newtext: http://google.co.uk 
No URL set!00000000 

从这:

if(isBinary($text)) { 
    echo "text: ".$text."\n"; 
    $newtext = binaryToText($text); 
    echo "newtext: ".$newtext."\n"; 
    curl_setopt($ch, CURLOPT_URL, $newtext); 
} else { 
    curl_setopt($ch, CURLOPT_URL, $text); 
} 
+0

检查'$ newtext = binaryToText($ text)' – user3791372

+0

的值嘿,我加了'$ newtext'的输出,就像它从echo中看到的那样(请参阅我的答案底部的编辑) –

回答

0

解决了!

我的$newtext变量中有一个NUL字节 - 因此,当echo ing工作时,cURL不接受它作为有效的URL。我用下面的代码行中我binaryToText()方法:

$return = str_replace("\0", "", $return); 

这将替换所有NULL字符与""。很高兴它的工作!

0

步行通过您的代码,做测试输出。

最可能的情况是您的isBinary()功能出现故障。

做使用

print_r(isBinary($text)) 

,看看它返回一个测试输出。

+0

我可以确认'isBinary()'按预期工作(我在发布之前删除了我的调试代码)。 –

+0

如果您有时间,请查看我的编辑。任何帮助表示赞赏! –