POST to url with cURL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Grizzzzzzzzzz
Forum Contributor
Posts: 125
Joined: Wed Sep 02, 2009 8:51 am

POST to url with cURL

Post by Grizzzzzzzzzz »

I'm attempting to write a simple script that given a name will automatically POST that information to another site, and return the results.

This is the site that i'm looking at:

http://euw.leagueoflegends.com/ladders/solo-5x5

and the form i'm interested in is
<form id="ladders-filter-form" method="post" accept-charset="UTF-8" action="/ladders/solo-5x5">
<div>
<input id="edit-submit" class="form-submit submit_button" type="submit" value="Search" name="op">
<div id="edit-player-wrapper" class="form-item">
<input id="edit-player" class="form-text" type="text" value="" size="60" name="player" maxlength="128">
</div>
<input id="edit-ladder-id" type="hidden" value="3" name="ladder_id">
<input id="form-d48014308d4389f847f6bb8bff3a28fa" type="hidden" value="form-d48014308d4389f847f6bb8bff3a28fa" name="form_build_id">
<input id="edit-ladders-filter-form" type="hidden" value="ladders_filter_form" name="form_id">
</div>
</form>
after playing around with it on my localhost, i've found that the highlighted elements are those that need to be POSTed, the other inputs don't seem to make a difference.

the php code i've thrown together to try and accomplish my aim is:

Code: Select all

	
        $fields = "op=Search&player=grizzzzzzzzzz&form_id=ladders_filter_form";
	$url = "euw.leagueoflegends.com/ladders/solo-5x5";

	$ch = curl_init($url);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
	$response = curl_exec($ch);
	curl_close ($ch);

	echo $response;
Which i believe should work... However, $response returns blank. If i remove the field "form_id=ladders_filter_form"i get a page response (but it hasn't performed a search as it requires the form_id)

Any advice or insight into why this is occuring would be appreciated.

Many thanks :)
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: POST to url with cURL

Post by Eric! »

Try adding some debug stuff to curl to see what is going on

Code: Select all

$debug=fopen("debug.txt","a");
$cookies = 'cookiejar.txt';
curl_setopt($process, CURLOPT_COOKIEFILE, $cookies);
curl_setopt($process, CURLOPT_COOKIEJAR, $cookies);
curl_setopt($process, CURLOPT_STDERR, $debug);
curl_setopt($process, CURLOPT_VERBOSE, TRUE);
Then you can look in the debug.txt to see what curl is doing. You might also benefit from loading an extension in firefox like live HTTP headers. Then you can browse the site, see how it submits to the page, and what it submits and duplicate that in cURL. Sometimes you might need to include additional headers with cURL like this, which you can get from looking at the headers when you submit the search

Code: Select all

$headers[] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
$headers[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$headers[] = "Connection: close";
$headers[] = "Content-type: text/html, application/x-www-form-urlencoded";
$user_agent = "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.17) Gecko/20110422 Ubuntu/10.10 (maverick) Firefox/3.6.17";
$refer="http://url.to.site.and.page/that/would/submit/this/form.php";
curl_setopt($process, CURLOPT_REFERER, $refer);
curl_setopt($process, CURLOPT_HTTPHEADER, $headers);
curl_setopt($process, CURLOPT_USERAGENT, $user_agent);
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: POST to url with cURL

Post by flying_circus »

Code: Select all

<?php
  # Define Vars
    $url = "http://euw.leagueoflegends.com/ladders/solo-5x5";
    $fields = array("op" => urlencode("Search"),
                    "player" => urlencode("grizzzzzzzzzz"),
                    "form_id" => urlencode("ladders_filter_form"));
	
  # Build Querystring
    foreach($fields AS $key => $value)
      $fields_string .= "{$key}={$value}&";
      
    $fields_string = 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
    $response = curl_exec($ch);
  
  # close connection
    curl_close($ch);
    
  # Print Response
    echo $response;
?>
Post Reply