0

I want to get data of a URL with sending some POST data. In simple word, as jQuery AJAX gets data, I want that in PHP. Similar of this code in PHP.

$.ajax({
    url: "http://example.com/",
    type: "POST",
    data: data,
    dataType: 'json',
    success: function (result) {
        alert(result);
    }
}); 
3
  • 1
    file_get_contents('your_target_url'); can get data from your requested url. Commented Jan 23, 2016 at 11:26
  • file_get_contents does not work with post - google for curl, (Curl URL Request Library) Commented Jan 23, 2016 at 11:38
  • Yes u can curl for this Commented Jan 23, 2016 at 11:45

1 Answer 1

1

you need to use curl for that. there is a great tutorial on that by davidwalsh here

//extract data from the post
//set POST variables
$url = 'http://domain.com/get-post.php';
$fields = array(
    'lname' => urlencode($_POST['last_name']),
    'fname' => urlencode($_POST['first_name']),
    'title' => urlencode($_POST['title']),
    'company' => urlencode($_POST['institution']),
    'age' => urlencode($_POST['age']),
    'email' => urlencode($_POST['email']),
    'phone' => urlencode($_POST['phone'])
);

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);
Sign up to request clarification or add additional context in comments.

Comments

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.