Sbbcarl Posted February 3, 2013 Share Posted February 3, 2013 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. Would be ideal if I could make it an array such as: Order --Token Order --Token Order --Token Any help on this would be appreciated! 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('"EC-', $do_order); $token = explode('"', $token[1]); $token = strip_tags($token[0]); $get_token = "EC-$token"; } } } Quote Link to comment Share on other sites More sharing options...
Sbbcarl Posted February 3, 2013 Author Share Posted February 3, 2013 Any ideas guys? I need to get past this point to finish the project :/ Quote Link to comment Share on other sites More sharing options...
kicken Posted February 4, 2013 Share Posted February 4, 2013 You're probably going to need to explain your problem better. Near as I can tell you're asking how to do a loop to download a bunch of URL's and extract some token from their source. The code you posted already appears to handle that successfully. If there is some issue you need to define what that issue is and probably provide some sample data such as the source of one of the pages your downloading and what it is you want to extract out of it. The only thing I see off hand that you might need is a call to curl_setopt in your downloadUrl function to set CURLOPT_COOKIEFILE to the same cookie.txt file you're using in the login function in order to carry over the login/session cookie, assuming there is one. Quote Link to comment Share on other sites More sharing options...
Sbbcarl Posted February 4, 2013 Author Share Posted February 4, 2013 (edited) Thanks for the reply When I run the function OrderURL $ch is passed with that from the login function so it should contain the cookie. The problem I'm having is the loop it self. If I echo the orders in the textarea I can see all the ones I type in. However, when those fun through the curl, it's only returning 1 token, not one for each order. It's something with the loop or how the variables should be within the loop to have it store as an array Edited February 4, 2013 by Sbbcarl Quote Link to comment Share on other sites More sharing options...
Christian F. Posted February 4, 2013 Share Posted February 4, 2013 It was a bit troublesome to spot, as the indentation in your code isn't the best. Don't know if that's because of the forum's editor, or if your code looks like that. If it's the latter, then I strongly recommend fixing it. In any case, from what I can see you're overwriting the token on each iteration of the loop. Meaning, only the last token will be used. Quote Link to comment Share on other sites More sharing options...
Sbbcarl Posted February 4, 2013 Author Share Posted February 4, 2013 Sorry , made the post on the go from iPad so it's not the best formatting How would I go about turning the token into an array to collect each one though the iterations ? Quote Link to comment Share on other sites More sharing options...
Christian F. Posted February 4, 2013 Share Posted February 4, 2013 http://php.net/array <- The PHP manual contains all of the information you need for that. Quote Link to comment Share on other sites More sharing options...
Sbbcarl Posted February 4, 2013 Author Share Posted February 4, 2013 Thanks, it's not that I don't know how to make the array, it's just because I'm scraping / exploring that data right before the value, does that need to be in some type of array to so it runs multiple times or just the end result? Quote Link to comment Share on other sites More sharing options...
kicken Posted February 4, 2013 Share Posted February 4, 2013 Add each token into an array as you gather them in the loop, then later on you can loop through the array of tokens to output them into your textarea. You can use the URL as the key for your array and put the token as the value that way you can easily find the token as you loop through the URLs when putting them into the textarea. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.