-1

I'm trying to send data via cURL post but I've never tried it before and I don't know if I'm doing it right.

What I want to do is sent a file via post to a file from my remote server and there read the file and insert data into database but unfortunately it's not working and error_log doesn't show me anything.

My code looks like this:

$ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,"https://".$host."/file.php");
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, array(
            'file' => '@'.realpath(../path/to/file/'.$_POST['file_name'].'.txt'),
            'action' => 'first',
            'check' => $_POST['file_name'],
        ));
        $result = curl_exec($ch);
        curl_close($ch);

This code is placed after some sql querys and code made to write this sql results into the file that I'm trying to send.

6
  • What do your receive from the server? Just print the $result variable. Also, you can read the HTTP status code $responseCode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE); before you close the connection. Or try without uploading the file Commented Jan 5, 2018 at 8:10
  • 1
    An issue occurs because CURLOPT_POSTFIELDS is supposed to be http_build_query($array), not an array. php.net/manual/ro/function.http-build-query.php Commented Jan 5, 2018 at 8:12
  • @Ruben it's the first time I'm using cURL and I don't understand how it really works. What my code is doing is putting that information via post in the CURLOPT_URL, right? Or is it getting that information via post from that url? Cause I'm understanding it like the first case. Commented Jan 5, 2018 at 8:13
  • Is there a missing quote in .realpath(../path/to/file/'.$_POST['file_name'].'.txt') - should there be one before ../path Commented Jan 5, 2018 at 8:18
  • @NigelRen you're right. Thanks for pointing that out! Commented Jan 5, 2018 at 8:23

1 Answer 1

-1

Here is the Answer by Shakir Khan you Should follow: PHP: upload file from one server to another server - Stack Overflow

OR you should read PHP Documentation carefully there is a huge amount of CONSTANT availabe to use: curl_setopt

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer. Looks like that's the right way to solve my problem. I'll try it later!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.