开发者

How do I prevent a redirect after using PHP cURL to upload images to another site?

A site has a form to allow uploading one image at a time. I use php cURL to upload multiple images from a zip file, which works.

I would like to see the curl_exec response and curl_getinfo data in the browser after the uploads are done. However, I am only able to see them for a split second before I am redirected to the site where the images were uploaded to:

$ch = curl_init($this->aData['urlData']['actionUrl']);

    foreach ($this->aData['images'] as $aFile)
    {
        $sUploadFile = $this->curlUploadFilename($aFile);

        $aPostData = array('sFromUrl' => $sFromUrl, 'token' => $sToken, 'uploadfile' => $sUploadFile, 'upload' => 1);

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        cur开发者_开发问答l_setopt($ch, CURLOPT_REFERER, $_SERVER['HTTP_REFERER']);
        curl_setopt($ch, CURLOPT_COOKIE, $sCookies);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $aPostData);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLINFO_HEADER_OUT, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);

        $aResponse = curl_exec($ch);

        if(curl_errno($ch) != 0)
        {
            $this->sErrorMsg = "POST cURL Error:\r\n" . curl_error($ch) . "\r\n";
            curl_close($ch);
            return false;
        }

        $aTempInfo[] = array('image' => $aFile, 'curlInfo' => curl_getinfo($ch), 'curlResponse' => $aResponse);
    }

    $this->aInfo = $aTempInfo;
    curl_close($ch);
    return true;

I have CURLOPT_FOLLOWLOCATION set to False and it still redirects. I am not in safe_mode and open_basedir is not set.

I have added CURLOPT_NOBODY as True and CURLOPT_CUSTOMREQUEST as POST (to keep request from switching to HEAD). I get different results:

  • If added after CURLOPT_POSTFIELDS, I am not redirected but the uploads fail.

  • If added before, uploads succeed but I am still redirected.

I know what CURLOPT_HEADER and CURLINFO_HEADER_OUT are supposed to do, but I set them both to False just to see if it makes a difference, but I am still redirected.


I know this is an old post, however i go ahead and answer it. I believe when you echo the results, there is some javascript in the response which is doing the redirect. Try by dumping the response to a file instead of in the browser and see if there is any javascript in there, if found remove the culprit part of the script doing str_replace() or something and then echo the output.


remove the URL from your curl_init(); and specify it using CURLOPT_URL:

curl_setopt($ch, CURLOPT_URL, $this->aData['urlData']['actionUrl']);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜