I am trying Curl in PHP for the first time, the reason is I want to scrape results from this page :http://www.lldj.com/pastresult.php . This site posts weekly lotto results since 2002 and has a simple submit form ( Date ).
A submit button : Name = Button / value = Submit
Select drop down : Name = Draw & Options #( 1 - 1097 ) // Represent draw number
I can go over it manually but i thought why don't i use a simple script and make it easier as I am also interested in testing how to submit data using PHP/ CURL and retrieve results.
I have used DOM PHP for scraping and I am comfortable using the syntax .
I wonder if I should use Curl and DOM together or this can be achieved with CURL.
What I have so far ;
include'dom.php';
$post_data['draw'] = '1097';
$post_data['button'] = 'Submit';
//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
$post_items[] = $key . '=' . $value;
}
//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);
//create cURL connection
$curl_connection =
curl_init('http://www.lldj.com/pastresult.php');
//set options
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT,
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
//perform our request
$result = curl_exec($curl_connection);
//show information regarding the request
print_r(curl_getinfo($curl_connection));
echo curl_errno($curl_connection) . '-' .
curl_error($curl_connection);
After Submitting Data / scrape
$t = $curl_connection->find('table',0); // ?? usualy referes to file_get_content Var
$data = $t->find('tr');
foreach($data as $n) {
$tds = $n->find('td');
$dataRows = array();
$dataRows['num'] = $tds[0]->find('img',0)->href;
var_dump($dataRows);
}
Can someone point on whether this is correct ? How can you set to automatically increase the submit value then repeat the process ( eg, submit darw = 1 then draw =2 ect. )
Thanks
<?php
while(true){
for($i=1;$i<5000;$i++){
$post_data['draw'] = $i; // will change every time like 1,2,3,4
$post_data['button'] = 'Submit';
//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
$post_items[] = $key . '=' . $value;
}
//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);
//create cURL connection
$curl_connection =
curl_init('http://www.lldj.com/pastresult.php');
//set options
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT,
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
//perform our request
$result = curl_exec($curl_connection);
//show information regarding the request
print_r(curl_getinfo($curl_connection));
echo curl_errno($curl_connection) . '-' .
curl_error($curl_connection);
// start your scrap
$t = $curl_connection->find('table',0); // ?? usualy referes to file_get_content Var
$data = $t->find('tr');
foreach($data as $n) {
$tds = $n->find('td');
$dataRows = array();
$dataRows['num'] = $tds[0]->find('img',0)->href;
var_dump($dataRows);
}
} for loop end here
}?>
Here just skeleton to use curl in continuously with changed id you can set it your way.
also please make sure to clear you variable after fetch data.
use like
...
curl_close($ch);
unset($fields_string);
...
Load the page
The prefered way to grab remote content is file_get_contents(). Use:
$html = file_get_contents('http://www.lldj.com/pastresult.php');
Thats's it.
Get content from the page
To get content from the page you will usually use DOMDocument and DOMXPath:
$doc = new DOMDocument();
#$doc->loadHTML($html);
$selector = new DOMXpath($doc);
// xpath query
$result = $selector->query('YOUR QUERY');
Related
i have Problem with my Scraping Script. I like to select the value from my sql by id in a while and save the the scraped content in my database. its works but the curl script send a wrong result to my database, everytime the content from the first request.
My Code:
<?php
error_reporting(E_ALL);
include('db.php');
$i = 1;
while ($i <= 5)
{
$sql = "SELECT * FROM `plz` WHERE `id` = '$i'";
$row = mysql_fetch_assoc( mysql_query($sql) );
$plz = $row['plz'];
//create array of data to be posted
$post_data['adv_plz'] = "$plz";
$post_data['finda'] = 'adv';
$post_data['lang'] = 'de_DE';
//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
$post_items[] = $key . '=' . $value;
}
//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);
//create cURL connection
$curl_connection = curl_init('https://www.domain.de/');
//set options
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
//perform our request
$result = curl_exec($curl_connection);
//show information regarding the request
$result = utf8_encode($result);
mysql_query("UPDATE plz SET content = '$result' WHERE id = '$i'");
$i++;
}
?>
Here is the Content of the echo $post_string
adv_plz=01000&finda=adv&lang=de_DE
adv_plz=01000&finda=adv&lang=de_DE&adv_plz=01001&finda=adv&lang=de_DE
adv_plz=01000&finda=adv&lang=de_DE&adv_plz=01001&finda=adv&lang=de_DE&adv_plz=01002&finda=adv&lang=de_DE
adv_plz=01000&finda=adv&lang=de_DE&adv_plz=01001&finda=adv&lang=de_DE&adv_plz=01002&finda=adv&lang=de_DE&adv_plz=01003&finda=adv&lang=de_DE
adv_plz=01000&finda=adv&lang=de_DE&adv_plz=01001&finda=adv&lang=de_DE&adv_plz=01002&finda=adv&lang=de_DE&adv_plz=01003&finda=adv&lang=de_DE&adv_plz=01004&finda=adv&lang=de_DE
I hope somebody can help me. If i try the script not in a while it works perfectly.
Try resetting the $post_items variable before entering the foreach loop.
$post_items = array();
//traverse array and prepare data for posting (key1=value1)
foreach ( $post_d ....
I'm getting the following error in when I'm trying to upload video ad:
Error Code : 1363030.
Your video upload timed out before it could be completed. This is probably because of a slow network connection or because the video you're trying to upload is too large. Please try again
The connection is fine and the video isn't too big .. chunk (1MB).
Any idea what the problem could be?
My code:
$transfer = array();
$transfer["access_token"] = "G4535PZXXX";
$transfer["upload_phase"] = "transfer";
$transfer["upload_session_id"] = $upload_session_id";
$transfer["start_offset"] = $start_offset";
$transfer["video_file_chunk"] = #$filepath;
print_r($transfer);
//traverse array and prepare data for posting (key1=value1)
foreach ($transfer as $key => $value) {
$post_items[] = $key . '=' . $value;
}
//create the final string to be posted using implode()
$post_string = implode('&', $post_items);
//create cURL connection
$curl_connection = curl_init('https://graph-video.facebook.com/v2.3/act_XXX/advideos');
//set options
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
//perform our request
$transfer_response = curl_exec($curl_connection);
//show information regarding the request
print_r(curl_getinfo($curl_connection));
echo curl_errno($curl_connection) . '-' . curl_error($curl_connection);
//close the connection
curl_close($curl_connection);
Edit:
To create the chunks I used this command
shell_exec('split --bytes='. $fb_response["end_offset"] . ' --suffix-length=1 --numeric-suffixes '. $this->filepath .' $chunkfilepath' );
As of PHP 5.5.0, the # prefix is deprecated and files can be sent using CURLFile.
see CURLOPT_POSTFIELDS # http://php.net/manual/en/function.curl-setopt.php
$video_file_chunk = new CURLFile($filepath,'video/mp4');
$transfer = array();
$transfer["access_token"] = "G4535PZXXX";
$transfer["upload_phase"] = "transfer";
$transfer["upload_session_id"] = $upload_session_id";
$transfer["start_offset"] = $start_offset";
$transfer["video_file_chunk"] = $video_file_chunk;
//create cURL connection
$curl_connection = curl_init('https://graph-video.facebook.com/v2.3/act_XXX/advideos');
//set options
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $transfer);
//perform our request
$transfer_response = curl_exec($curl_connection);
//show information regarding the request
print_r(curl_getinfo($curl_connection));
echo curl_errno($curl_connection) . '-' . curl_error($curl_connection);
//close the connection
curl_close($curl_connection);
For whomever finds this later, this Facebook error occurs if you don't specify the content type and file name of the attached file (i.e. if you pass it as an inline field value, not as an attached file).
I don't use PHP often, so can't really give the correct curl command, but the code above seems to have a bug because it uses # operator (“suppress errors”) instead of passing an #-prefixed file name to curl. The corresponding line should read:
$transfer["video_file_chunk"] = '#' . $filepath;
Not sure if this is enough to fix the overall issue in PHP.
my problem is that i'm working with salesforce in wordpress, i'm not using wordpress-to-lead plugin, I have a form in a template and that form sends data to salesforce via cURL and also is posting data in database cause I have to generate a password and then send it to the user but its not working, is working salesforce but not saving data in the database, here is my code to post data in database and generate the password
$keysString = implode(", ", array_keys($blank_section));
unset($_POST['userId']);
$user_id = mysql_query("SELECT MAX(id) AS id FROM int_form_data");
$user_id = $user_id+11;
$passwordSend = 'INTELIGOS'.rand(10000, 5000000);
$array_user_id = array('user_id' => $user_id, 'password' => $passwordSend);
$posted_data = array_merge($_POST,$array_user_id);
foreach($posted_data as $k=>$v) {
$itfdatainfo[$k] = $v;
}
$itfkeys = array_keys($itfdatainfo);
$itfvalues = array_values($itfdatainfo);
if(isset($_POST['submit'])) {
$sql = "INSERT INTO int_form_data (".implode(',',$itfkeys).") VALUES('".implode("','",$itfvalues)."')";
$result = mysql_query($sql);
}
And here I use cURL to send data to Salesforce:
//Initialize the $query_string variable for later use
$query_string = "";
$kv = array();
//If there are POST variables
if ($_POST) {
//Initialize the $kv array for later use
//For each POST variable as $name_of_input_field => $value_of_input_field
foreach ($_POST as $llav => $value) {
//Set array element for each POST variable (ie. first_name=Arsham)
$kv[] = stripslashes($llav)."=".stripslashes($value);
}
//Create a query string with join function separted by &
$query_string = join("&", $kv);
}
//Check to see if cURL is installed ...
if (!function_exists('curl_init')){
die('Sorry cURL is not installed!');
}
//The original form action URL from Step 2
$url = 'https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8';
//Open cURL 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($kv));
curl_setopt($ch, CURLOPT_POSTFIELDS, $query_string);
//Set some settings that make it all work
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//Execute SalesForce web to lead PHP cURL
$result = curl_exec($ch);
//close cURL connection
curl_close($ch);
Anyone knows why is happening that? I have all the code in one template in wordpress
I'm looking at this INSERT INTO int_form_data (".implode(',',$itfkeys).") and thinking if some column name in $itfkeys needs to be enclosed in backticks then it would cause an sql error.
I am collecting form posts via jQuery via the.val() method, validating those posts and passing errors back to the form inputs, or passing true to the .$post method which invokes a PHP cURL script.
A typical var will look like this:
var industs_servedVal = $('#industs_served').val();
In this case it is a select multiple form field. I understand that the .val() method in jQuery passes an array, so that seems reasonable, and am I right in saying that the var will also collect the array.
I then pass var industs_servedVal to the $.post method like this ( then slide up a thank you note):
$.post('../inc/form_sendsf_modified.php', {
othervars: othervarsVal;
industs_served: industs_servedVal,
}, function(data) {
$('#sendEmail').slideUp('slow', 'swing', function() {
$('#sendEmail').replaceWith('<h3>Thank you!</h3><p>Your message was sent to us. We\'ll get back to you as soon as we can.</p>');
});
});
}
return false;
});
The file "form_sendSF_modified.php" handles those posts and sends to the Sales Force Cloud using cURL. This works; however, the problem is that Sales Force shows "array" as the values received for the multiple field array, not the array values themselves. Is there a problem in the way I am collecting the array and passing it to sales force. Is the foreach loop capable of sending the multiple field array values as well as the other values as an array as shown in the code.
$post_data['00N70000002U2fA'] = $_POST['industs_served']; //Array
//$otherpost data
//cURL CODE for post
//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
$post_items[] = $key . '=' . $value;
}
//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);
//create cURL connection to SFDC
$curl_connection = curl_init('https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8');
//set options
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT,
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
//perform our request
$result = curl_exec($curl_connection);
//show information regarding the request
//print_r(curl_getinfo($curl_connection));
//echo curl_errno($curl_connection) . '-' .
curl_error($curl_connection);
//close the connection
curl_close($curl_connection);
//End cURL
You can use array itself (but it will change Content-Type header to multipart/form-data).
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_data);
Or you can use build string with http_build_query function, look example #3.
use this
$post = "ac=on&p=1&pr[]=0&pr[]=1&a[]=3&a[]=4&pl=on&sp[]=3&ct[]=3&s=1&o=0&pp=3&sortBy=date";
parse_str($post,$fields);
$url = 'http://example.com/';
//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, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
I'm working with this API here: https://gatewaydtx1.giact.com/gVerifyV2/POST/Verify.asmx?op=Call using curl in php. I am able to do a test just fine on a single call to the API. However when I try to loop over several records, I get an error on every attempt after the first one.
Here's my code:
<?
//set the variables for posting
$CompanyID = "123";
$Token = "013443234-224e-4f46-bad4-6693deae2231";
$CheckNumber = "1";
$Amount = "30";
$UniqueID = "111";
$url = "https://gatewaydtx1.giact.com/gVerifyV2/POST/Verify.asmx/Call";
//Get the records from table
$sql = "SELECT id,account_no,routing_no FROM banktable WHERE(status = 'queued') LIMIT 0,100";
$result = mysql_query($sql) or die("Error: " . mysql_error() . "<br>");
while($row = mysql_fetch_array($result)) {
$RoutingNumber = $row['routing_no'];
$AccountNumber = $row['account_no'];
//Do the curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_URL, $url );
$post_array = array(
"CompanyID"=>$CompanyID,
"Token"=>$Token,
"RoutingNumber"=>$RoutingNumber,
"AccountNumber"=>$AccountNumber,
"CheckNumber"=>$CheckNumber,
"Amount"=>$Amount,
"UniqueID"=>$UniqueID,
);
//url-ify the data
foreach($post_array as $key=>$value){
$post_array_string .= $key.'='.$value.'&';
}
$post_array_string = rtrim($post_array_string,'&');
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_POST,count($post_array ));
curl_setopt($ch,CURLOPT_POSTFIELDS,$post_array_string);
$response = curl_exec($ch);
echo $response;
curl_close($ch);
}
?>
And here's what this code outputs after looping 4 rows:
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://www.giact.com/webservices/gVerifyV2/">33302261|true|No Data|ND00</string>
Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).
Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).
Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).
Note that the first record it tried produced a correct result. After that, the errors. And even though I mention my loop specifically here, I should note that this also happens if I simply hard code two or more curls on the page.
//url-ify the data
foreach($post_array as $key=>$value){
$post_array_string .= $key.'='.$value.'&';
}
$post_array_string = rtrim($post_array_string,'&');
I think you need to clear the $post_array_string variable in each loop.
unset($post_array_string);
Before:
foreach($post_array as $key=>$value){
Add:
$post_array_string = '';
Or you can use http_build_query() function.