preg_match on cURL?

arconis

Used Receptacle
Oct 30, 2008
114
0
0
41
Hey guys, I'm using a social login API and I'm trying to grab the XML details for a specific user I already have the token of:

Code:
<?php

  //Your Site Settings
  $site_subdomain = 'mywebsite';
  $site_public_key = 'mystuff';
  $site_private_key = 'mystuff';

  //API Access Domain
  $site_domain = $site_subdomain.'.api.com';

  //Connection Resource
  $resource_uri = 'https://'.$site_domain.'/users/'.$token.'.xml';

  //Setup connection
  $curl = curl_init();
  curl_setopt($curl, CURLOPT_URL, $resource_uri);
  curl_setopt($curl, CURLOPT_HEADER, 0);
  curl_setopt($curl, CURLOPT_USERPWD, $site_public_key . ":" . $site_private_key);
  curl_setopt($curl, CURLOPT_TIMEOUT, 15);
  curl_setopt($curl, CURLOPT_VERBOSE, 0);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 1);
  curl_setopt($curl, CURLOPT_FAILONERROR, 0);

  //Send request

  $result_xml = curl_exec($curl);
  curl_close($curl);

  //Done
  echo "$result_xml";


?>

This brings back a XML of everything I need, I'm just having trouble scraping the data between

Code:
<displayName>Awesome Guy</displayName>
and

Code:
<emails>
              <value>superduper@emailhere.com</value>

I've been to every cURL tutorial site and can't seem to figure it out, all the regular expressions are throwing me off...anything helps

oh, and yes, I have to use cURL the site said....can't fetch or use DOM
 


you don't need regex for that bro, at least not any COMPLEX regex, you can just escape the characters that need escaping (like the forward slash) and put your (.*) between those tags (replacing "Awesome Guy" or that email address).
 
Thanks, why does this return no matches though?

Code:
 //Done
  echo "$result_xml";

$str = "$result_xml";
$regexp = '<displayName>.*<//displayName>';

if(preg_match_all($regexp, $str, $matches)) {
    for($i = 0; $i < count($matches[0]); $i++) {
        echo "Match #$i = {$matches[1][$i]}\n<br>";
    }
}
else {
    echo "No matches.";
}


Sorry, new to this :/
 
iamjon is right, use xml.

But this regex is very useful in general:

Code:
<tagname.*>(.+?)<

but xpath is really the shit.
 
God damnit, that xpath looks like exactly what coding languages were made for...ease..But alas, I can't get it to work, I tried xpath which looks foolproof but they don't know this fool.

The modified expression from mattseh is returning No Matches, so it might be some other issue completely....

Blah, I really need to see someone else to code it properly and backwards-analyze it to learn it. Where can I post small PHP jobs like this? Freelancer.com only does 1 free post right? I'm gonna need an account since it's a rather complicated site I'm making. What's the max it should cost to grab 10 variables from an XML and INSERT into database?

I don't like doing it, but it's been 3 days and I need to move on to other features...blah
 
if you can post the XML here, I or someone else will show you the needed xpaths.

yoda.jpg
 
Code:
function string_between($string, $start, $end){
    $string = " ".$string;
    $ini = strpos($string,$start);
    if ($ini == 0) return "";
    $ini += strlen($start);
    $len = strpos($string,$end,$ini) - $ini;
    return substr($string,$ini,$len);
}

$name = string_between($result_xml,"<displayName>","</displayName>");
 
I fucking love you dooogen, +rep

Thanks for the offer dchuk :) I'm glad I don't have to put you guys outta your way now.
 
Oh shit, I didn't think to use them for programming/coding.

Thx for xpath information, I'll research into it when I'm more fluent with PHP.