简体   繁体   中英

Submit button of remote site in PHP cURL

Is there any way to trigger an onClick event over a button and get the result with cURL PHP or any other method?

<button class="yt-uix-button yt-uix-button-size-small yt-uix-buttondefault business-email-button" type="button" onclick=";return false;">
    <span class="yt-uix-button-content">View email address</span>
</button>

You cannot directly simulate a click with CURL. You have to analyse the request (HTTP Headers) you send to the server, when you click on the button, with tools like Chrome DevTools. You can use it like this .

Once you know the request URL and the parameters (if they exist), you can build a CURL request and get the response like this:

$ch = curl_init();

// params example
$post = [
    'username' => 'user1',
    'password' => 'pass1'
];

// Request URL
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

// server response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$server_response = curl_exec ($ch);

curl_close ($ch);

Yes there are few ways to do it. First check out the CURL Methods Posted here. http://hayageek.com/php-curl-post-get/

You will then have to Create a Script using Ajax or jQuery for button onClick, I will post two examples for jQuery and Javascript below.

jQuery with PHP

1) First save this as your HTML file

<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<script type="text/javascript" language="javascript">
 $(document).ready(function() {
   $("#name").click(function(event){
     $.post("jQueryPost.php",
    {name: "Miguel", surename: "Makonnen"},
    function(data) { 
    $('#result').html(data);}
      );
   });
 });
</script>
<div id="result">
</div>
<input type="button" id="name" value="Check Name" />

2) Then save this as your remote PHP file and name it jQueryPost.php

<?php
if($_POST["name"]) {
   $NAME = $_POST['name'];
   $SURENAME = $_POST['surename'];
   echo "Name ".$NAME."<BR>"; 
   echo "Surname: ".$SURENAME."<BR>"; 
}
?>

Using "onClick" the script above will send a POST request with a Name + Surename to the Remote PHP file. The Remote PHP file will process the Request and send it as a Callback to your HTML File. You can change content of the PHP to anything you want.

Javascript AJAX with PHP

1) First this as your HTML File.

<script type="text/javascript">
function XmlHttp_Request() {
  var xmlHttp = null;
  if(window.XMLHttpRequest) {       // Firefox, IE7+, Opera Support
    xmlHttp = new XMLHttpRequest();
  }
  else if(window.ActiveXObject) {   // fInternet Explorer 5, 6 Support
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  }
  return xmlHttp;
}

// Post Ajax Data
function ajaxPost(Filename, Finalise) {
  var request =  XmlHttp_Request(); 

  // Post Parameters
  var  postData = 'name='+document.getElementById('myname').innerHTML;
  request.open("POST", Filename, true); // Our Post tRequest
  request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  request.send(postData);       // Post Data

  // Check request status
  request.onreadystatechange = function() {
    if (request.readyState == 4) {
      document.getElementById(Finalise).innerHTML = request.responseText;
    }
  }
}
</script>

<button onclick="ajaxPost('ajaxPost.php', 'finalise')">Check Name</button>
<div id="myname"></div>
<div id="finalise"></div>

2) Then save this as as your remote PHP file named ajaxPost.php .

<?php
if (isset($_POST['name'])) {
   echo "Name:    Miguel <BR>"; 
   echo "Surname: Makonnen"; 
}
?>

This will simply send a Response based on Ajax Request with a Name + Surname from the remote PHP file on Button Click. Like I've mentiond, you can change the content of the PHP with a Curl script. It shouldnt be that hard. Good luck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM