Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
412 views
in Technique[技术] by (71.8m points)

PHP: substr returns empty string

The substr function in my PHP file has an empty string as a result.

Here is my PHP code:

    <?php
$requestArray = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"User-agent: MKUxA&8PtUYP(d3So)pfPSIvD5cf75"
  )
);

$stream = stream_context_create($requestArray);
$streamResult = file_get_contents('https://beoordelingen.feedbackcompany.nl/samenvoordeel/scripts/flexreview/getreviewxml.cfm?ws=9673&publishIDs=0&nor=0&publishDetails=0&publishDetailScores=0&publishOnHold=0&sort=desc&foreign=1&v=3&publishDetailScores=1&Basescore=10', false, $stream);

$str = substr($streamResult, 0, 3);
var_dump($str);

echo "<br>";
var_dump($streamResult);
echo "<br>";
var_dump($http_response_header);

?>

This is the result:

string(3) "string(251) " 8.7109https://beoordelingen.feedbackcompany.nl/NL-NL/De%2DMariannehoeve.html "
array(6) { [0]=> string(15) "HTTP/1.1 200 OK" [1]=> string(37) "Content-Type: text/xml; charset=UTF-8" [2]=> string(25) "Server: Microsoft-IIS/7.5" [3]=> string(21) "X-Powered-By: ASP.NET" [4]=> string(35) "Date: Mon, 22 Feb 2016 13:39:34 GMT" [5]=> string(17) "Connection: close" }

Could anyone tell me what I'm doing wrong and help me out?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The content is correctly returned, but you see above result in browser because the browser interpret the tags.

Your result is:

<?xml version="1.0" encoding="UTF-8" ?>
    <rating><score>8.7</score><scoremax>10</scoremax><noReviews>9</noReviews><detailslink>https://beoordelingen.feedbackcompany.nl/NL-NL/De%2DMariannehoeve.html</detailslink><reviewDetails></reviewDetails></rating>

So, substr($streamResult, 0, 3) is <?x, that — rendered — is:

Also, $streamResult is above XML that — rendered — is:

8.7109https://beoordelingen.feedbackcompany.nl/NL-NL/De%2DMariannehoeve.html

If you want see the result in the browser, write this:

 echo htmlentities( $streamResult );

Edit:

The returned data is XML, so — to parse it — you have to use DOMDocument (or another parser):

$dom = new DOMDocument();
$dom->loadXML( $streamResult );
$score = $dom->getElementsbyTagName( 'score' )->item(0)->nodeValue;

echo $score;

The example abowe will output:

8.7


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...