Getting 404 Error while inserting the data by cURL in php

26 Views Asked by At

Here I wrote the code to insert the data by cURL in php, to send the data to Moodle database from php server to moodle server I used the cURL as below code

$data = array(
    'auth'=>'Manual',
    'confirmed'=>1, 
    'mnethostid'=>1,
    'username' =>$email_address,
    'password'=>password_hash('$password', PASSWORD_DEFAULT),
    'firstname'=>$first_name,
    'lastname'=>$last_name,
    'email'=>$email_address,
    'country'=>$country,
    'address'=>$full_address1
);
echo "<pre>"; print_r($data); 
$data_json = json_encode($data);
echo "<pre>"; print_r($data_json);
$url = 'http://localhost/moodles/server/moodle/user/editadvanced.php?id=-1';
                       
$headers = [
    'X-Apple-Tz: 0',
    'X-Apple-Store-Front: 143444,12',
    'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    'Accept-Encoding: gzip, deflate',
    'Accept-Language: en-US,en;q=0.5',
    'Cache-Control: no-cache',
    'Content-Type: application/x-www-form-urlencoded; charset=utf-8',
    'Host: www.example.com',
    'Referer: http://www.example.com/index.php', // Your referrer address
    'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:28.0) Gecko/20100101 Firefox/28.0',
    'X-MicrosoftAjax: Delta=true'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION, true);    // follow redirects
curl_setopt($ch,CURLOPT_ENCODING, "");     // handle all encodings
// curl_setopt($ch,CURLOPT_USERAGENT, "spider"); // who am i
curl_setopt($ch,CURLOPT_AUTOREFERER, true);     // set referer on redirect
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,15);     // timeout on connect
curl_setopt($ch,CURLOPT_TIMEOUT,15);      // timeout on response
curl_setopt($ch,CURLOPT_MAXREDIRS,10);       // stop after 10 redirects
$userpwd  = base64_encode($uname.":".$pass);
curl_setopt($ch, CURLOPT_HEADER,true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authentication : Basic','Content-Type: application/json'));
curl_setopt($ch, CURLOPT_USERPWD, $userpwd);
//'Content-Length: ' . strlen($data_json)));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTREDIR, 3);
curl_setopt($ch,CURLOPT_HTTPAUTH,CURLAUTH_DIGEST);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if(curl_exec($ch) === false)
{
    echo 'Curl error: ' . curl_error($ch);
} else {
    $response  = curl_exec($ch);
}
echo "<br>";
var_dump($response); exit;
curl_close($ch);
$json_a2 = json_decode($response, true);

And i am facing the issue as below screen shot enter image description here

Can you please help me to sort out the issue!

Thanks.

I tried in cURL in php for insert the data from one server to another server i wrote the code in my php project and trying to insert the data into moodle cms project.

0

There are 0 best solutions below