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.
Related
I have the following data I pulled from an API using PHP's file_get_contents($url), I would like to parse this data and put each comma separated value into a variable whilst looping to the next set (there are thousands of row, I simply extracted the first two), I have tried parsing methods however most have an element consisting of the datalabel:data, any assistance would be gladly appreciated.
[[1610223840000,"3.63410000","3.65100000","3.62900000","3.64150000","14194.01000000",1610223899999,"51684.84892800",195,"7619.89000000","27756.15839400","0"],[1610223900000,"3.64610000","3.65090000","3.63410000","3.65000000","2219.73000000",1610223959999,"8090.68646600",46,"1176.75000000","4290.44934900","0"]]
Ok figured this out today, for anyone who needs it
$ch = curl_init();
$url = "https://api.blahblahblah";
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
curl_close($ch);
$obj = json_decode($result, true);
foreach ($obj as $key => $value) {
echo $value[0] . "<br>";
echo $value[1] . "<br>";
}
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 ....
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 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');
I am trying to add the results from the while loop into an xml request. But it is not showing up correctly.. Below I first create a function, then I try to make that function work inside the xml request curl tag. It is hard to explain and easier to see:
//Getting room groups
function roomGroups(){
$i = 2;
while(isset($_GET['ad'.$i]))
{
$adult = $_GET['ad'.$i];
$child = $_GET['ch'.$i];
$childAge = $_GET['ch'.$i];
echo "<RoomGroup><Room><numberOfAdults>".$adult."</numberOfAdults><numberOfChildren>".$child."<childAges>".$childAge."</childAges></Room></RoomGroup>";
$i++;
}}
//Room availablility Request
$ch1 = curl_init();
$fp1 = fopen('room_request.xml','w');
curl_setopt($ch1, CURLOPT_URL, "http://api.ean.com/ean-services/rs/hotel/v3/avail?cid=379849&minorRev=13&apiKey=4sr8d8bsn75tpcuja6ypx5g3&locale=en_US¤cyCode=USD&customerIpAddress=67.20.125.193&customerUserAgent=Mozilla/5.0+(Windows+NT+6.1)+AppleWebKit/535.11+(KHTML,+like+Gecko)+Chrome/17.0.963.79+Safari/535.11&customerSessionId=0ABAA856-8502-E913-6982-E2210F904B72&xml=<HotelRoomAvailabilityRequest><hotelId>".$hid."</hotelId><arrivalDate>".$arrivalDate."</arrivalDate><departureDate>".$departingDate."</departureDate><RoomGroup><Room><numberOfAdults>".$adults."</numberOfAdults><numberOfChildren>".$children."</numberOfChildren><childAges>".$ages."</childAges></Room></RoomGroup>".roomGroups()."<includeDetails>true</includeDetails></HotelRoomAvailabilityRequest>");
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch1, CURLOPT_HTTPHEADER, array('Accept: application/xml'));
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_setopt($ch1, CURLOPT_FILE, $fp1);
$val1 = curl_exec($ch1);
curl_close($ch1);//Close curl session
fclose($fp1); //Close file overwrite
$avail = simplexml_load_file('room_request.xml');
//The url that is passing the data looks like this: /hotel_request.php?hid=370111&d=05/02/2012&a=04/30/2012&r=3&ad2=2&ch2=22=16,17,&ad3=3&ch3=33=7,13,16,&
I get nothing back. Any help on this would be GREATLY appreciated!
roomGroups() doesn't return anything, it just echos to the screen.
Try something like this:
function roomGroups(){
$i = 2;
$ret = '';
while(isset($_GET['ad'.$i])){
$adult = $_GET['ad'.$i];
$child = $_GET['ch'.$i];
$childAge = $_GET['ch'.$i];
$ret .= "<RoomGroup><Room><numberOfAdults>".$adult."</numberOfAdults><numberOfChildren>".$child."<childAges>".$childAge."</childAges></Room></RoomGroup>";
$i++;
}
return $ret;
}