Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-03-2013, 08:24 PM   PM User | #1
carlhussey
New to the CF scene

 
Join Date: Dec 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
carlhussey is an unknown quantity at this point
Exclamation CURL inside Loop

Hey Guys,

I need a little help with this. I have a CURL function set up and I'm trying to collect multiple order numbers from a textarea and run them through the curl to get results for each order.

I'm not sure how the loop should be formatted to return / contain the results for each order.

I need $get_token to contain the results for each order number in the textarea.

Any help on this would be appreciated!



Code:
function downloadUrl($Url, $ch){
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_REFERER, "https://website here");
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);


$output = curl_exec($ch);


return $output;
}



function login($user,$pass){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://website here login'); //login URL
curl_setopt ($ch, CURLOPT_POST, 1);
$postData="
node=security
&static_password=$pass
&authorize=Proceed
&mac_admin_name=$user
&static_password_text=$pass";
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);


$store = curl_exec ($ch);


return $ch;
}



function getOrder($order_n,$ch){
     $order_url = downloadUrl("Website here", $ch);
     $stripped_html = str_replace("\t", '', $order_url);

         return $stripped_html;
}





if ($_POST['submit'] == "Search Orders"){

$order_n = trim($_POST['orders']);


     if ($order_n == ""){


$err = "<font color=\"red\"><i>Error: You must provide an order number!</i></font><br />";


}else{



$lines = explode("\n", $order_n);


     foreach( $lines as $line ){

     $user = "username";
     $pass = "password";
          $ch=login($user,$pass);
                 $do_order = getOrder($line,$ch);

     $token = explode('&quot;EC-', $do_order);
     $token = explode('&quot;', $token[1]);
     $token = strip_tags($token[0]);
     $get_token = "EC-$token";
}


}


}
carlhussey is offline   Reply With Quote
Old 02-05-2013, 09:32 PM   PM User | #2
ste.richards
New to the CF scene

 
Join Date: Oct 2011
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
ste.richards is an unknown quantity at this point
It sounds like you've done all the hard work (getting the data) and now want to store it nicely?

I'd add the data in to an array, using the token as an array key.lo

In your foreach loop, add the following at the bottom:

PHP Code:
$data[$token] = $doOrder
ste.richards is offline   Reply With Quote
Old 02-07-2013, 01:45 PM   PM User | #3
Redcoder
Regular Coder

 
Redcoder's Avatar
 
Join Date: May 2012
Location: /dev/couch
Posts: 309
Thanks: 2
Thanked 46 Times in 45 Posts
Redcoder has a little shameless behaviour in the past
Sidenote: Look into cURLs multiple Url download which makes the requests as slow as the slowest mdownload if you feel you'd like to optimize the script instead of having the script fetch a sibgle url for every run of the loop.
__________________
For professional Hosting and Web design.....


NetEssentials.co.uk
Redcoder is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:13 PM.


Advertisement
Log in to turn off these ads.