使用curl上传链接


Using curl to upload link

我需要你的帮助,因为我不知道这个简单代码的问题在哪里。

最初的代码是有效的,但在第二个代码上,它不起作用。

<?php
    $fast = json_decode(file_get_contents('http://www.server.org/api/get-server'));
    if($fast->error !== 'success') die('Error!');
    $url = file_get_contents('http://image.com/logo.png');
    $tmpfile = tempnam("/tmp", "FOO");
    $filearray = array('files[]'=>'@'.$tmpfile);
    $handle = fopen($tmpfile, "w");
    fwrite($handle, $url);
    fclose($handle);
    $file = array('files[]'=>'@'.$tmpfile);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$fast->server);
    curl_setopt($ch, CURLOPT_POST,1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $filearray);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $result = curl_exec($ch);
    curl_close ($ch);
    $return = json_decode($result, true);
    print_r($return);
    echo $return[0]['url'] . PHP_EOL;
?>

它工作得很好,但我只需要在这个网站上发布一个带有参数的url。

这里有一个请求的例子:

Post:server.com/api/remote-upload?link=my_url_link

所以我修改了这样的代码,不要在我的服务器上打开文件:

    <form method="post" action=""> 
        Url : <input type="text" name="link" size="12"><br> 
              <input type="submit" value="OK"> 
    </form>
<?php
    $my_link = $_POST['link']; 
    $fast = json_decode(file_get_contents('http://www.server.org/api/get-server'));
    if($fast->error !== 'success') die('Error!');
    $filearray = array('link'=>$my_link); //param
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$fast->server);
    curl_setopt($ch, CURLOPT_POST,1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $filearray);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $result = curl_exec($ch);
    curl_close ($ch);
    $return = json_decode($result, true);
    print_r($return);
?>

什么也没回。。结果是Array()

我有测试变量,但我不知道为什么是不工作?

我已经修复了我的代码。

这是一个简单的代码,但它非常适合大文件或任何格式:

$links = $_REQUEST['link']; //value of param
$api = "http://www.server.org/api/remote-upload"; //api server 
    $post = array('link' => $links);//param ?link=link you want to upload
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$api);
    curl_setopt($ch, CURLOPT_POST,1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    $result=curl_exec ($ch);
    curl_close ($ch);
    echo $result;