I want to parse this page with php. I wrote this code, but it gives me an error - Invalid argument supplied for foreach()
$opts = array('http' => array('header' => 'Accept-Charset: UTF-8, *;q=0'));
$context = stream_context_create($opts);
$data = file_get_contents('http://cbr.ru/scripts/XML_daily.asp',false, $context);
$xml = simplexml_load_string($data);
foreach($xml->valcurs->valute as $val){
echo "<p>".$val->attributes()->numcode."</p>";
}
Try this
foreach($xml->Valute as $val){
echo "<p>".$val->NumCode."</p>";
}
Might be the header then:
$opts = stream_context_create(array('http' => array('header' => 'Accept:
application/xml')));
Still think you shouldn't grab attributes() though:
foreach($xml->ValCurs->Valute as $val) {
echo "<p>".$val->NumCode."</p>";
}
Related
I'm trying to get links from the site.
"http://www.perfumesclub.com/es/perfume/mujer/c/"
For this use "user-agent" in simple html Sun.
But I get this error ..
Fatal error: Call to a member function find() on string in C:\Users\Desktop\www\funciones.php on line 448
This is my code:
Thanks^^
$url = 'http://www.perfumesclub.com/es/perfume/mujer/c/';
$option = array(
'http' => array(
'method' => 'GET',
'header' => 'User-Agent: Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
)
);
$context = stream_context_create($option);
$html = new simple_html_dom();
$html = file_get_contents ($url, false, $context);
$perfumes = $html->find('.imageProductDouble'); --> this is line 448
foreach($perfumes as $perfume) {
// Get the link
$enlaces = "http://www.perfumesclub.com" . $perfume->href;
echo $enlaces . "<br/>";
}
wrap your file_get_contents in str_get_html function
// method 1
$html = new simple_html_dom();
$html->load( file_get_contents ($url, false, $context) );
// or method 2
$html = str_get_html( file_get_contents ($url, false, $context) );
you're creating a new dom and assigning it to the variable $html, than reading the url returning the string and setting it to $html, thus overwriting your simple_html_dom instance, so when your invoking the find method you have a string instead of an object.
$html is a string after the call to file_get_contents. Try
$html = file_get_html($url);
OR use
$html = str_get_html($html);
after the call to file_get_contents.
i have a chriskacerguis Rest Server ,that listen for a client request as usually an API Server do.
base on client request i want to send/response some data to client in header only.
my questions are:
how do i access Client header first? then
how do i set Header in Rest Server?
This is how i send a request to REST SERVER:
function request_curl($url = NULL) {
$utc = time();
$post = "id=1&CustomerId=1&amount=2450&operatorName=Jondoe&operator=12";
$header_data = array(
"Content-Type: application/json",
"Accept: application/json",
"X-API-KEY:3ecbcb4e62a00d2bc58080218a4376f24a8079e1",
"X-UTC:" . $utc,
);
$ch = curl_init();
$curlOpts = array(
CURLOPT_URL => 'http://domain.com/customapi/api/clientRequest',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $header_data,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $post,
CURLOPT_HEADER => 1,
);
curl_setopt_array($ch, $curlOpts);
$answer = curl_exec($ch);
// If there was an error, show it
if (curl_error($ch)) {
die(curl_error($ch));
}
curl_close($ch);
echo '<pre>';
print_r($answer);
echo '</pre>';
}
Below is my REST SERVER function that listen request and will response a header:
public function clientRequest_post() {
// Getting Post Data
$entityBody = file_get_contents('php://input', 'r');
$this->response($entityBody,200);
//getting header data ,no idea
}
May be try php function getallheaders() which will fetch all the header data for you. If you want to convert it into array, use foreach.
So this will get you the header data and will convert it into array
$headers=array();
foreach (getallheaders() as $name => $value) {
$headers[$name] = $value;
}
Now if you want to get body and convert it into array as well
$entityBody = file_get_contents('php://input', 'r');
parse_str($entityBody , $post_data);
The final function will look something like this...
public function clientRequest_post() {
$headers=array();
foreach (getallheaders() as $name => $value) {
$headers[$name] = $value;
}
$entityBody = file_get_contents('php://input', 'r');
parse_str($entityBody , $post_data);
$this->response($entityBody, 200);
}
Btw, I assume $this->response($entityBody,200); will generate the response for you. Best of luck with it
I have the following class, it's using a web service to send a request and get a JSON response back.
When i try the same thing with Chrome's Postman extension, I get a nice little response back.
class RequestController {
function __construct() {
global $config;
$this->config = $config;
}
function send_request() {
$url = $this->config['GATEWAY']['url'] . 'getbyquery';
$json_data = '{"query":{"AllFields":true,"ConditionSetOperator":0,"ConditionSets":[],"Distinct":false,"Fields":["jay_alb","jay_levelms"],"Links":[],"Orders":[],"RecordType":"jay_lifeinsurance","Top":0}}';
$data = array($json_data);
$options = array(
'http' => array(
'header' => "Content-type: application/json\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
print "<pre>";
print_r($options);
print "</pre>";
$result = file_get_contents($url, false, $context);
$data = json_decode($result);
var_dump($data);
}
}
However when I'm using my class I get an error.
Warning: file_get_contents(http://crm-gateway.premier.com.au/GatewayService.svc/getbyquery) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 411 Length Required in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\crm\includes\classes\RequestController.php on line 32
Can you please point out what I'm doing wrong?
Thanks,
I am facing one issue while i am calling rest service using file_get_contents
its working fine when response is success but its giving blank result in case of failure or error response. while when I am checking it using rest client its giving me correct response for both case whether its success or failure.
can anyone please help? below is the source code which i have written.
<?php
$postdata = array(
'email' => $email,
'password' => $password
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => $headers = array(
'Accept: application/json',
'Content-Type: application/json'
)
//'content' => $postdata
)
);
$context = stream_context_create($opts);
//echo "<pre>"; print_r($context); exit;
$url = WS_URL . "issavvy-api/account/login?" . http_build_query($postdata);
$result = file_get_contents($url, false, $context);
echo "<pre>";
print_r(json_decode($result));
exit;
?>
If you wanna stick with file_get_contents use $http_response_header and parse it
You can use this
function httpStatusCode($headers){
$match = null;
$pattern = "!(?P<version>HTTP/\d+\.\d+) (?P<code>\d+) (?P<status>.*)!";
foreach($headers as $header){
if(preg_match($pattern, $header, $match)){
return $match['code'];
}
}
return null;
}
To check if request was successful run
$success = (200 == httpStatusCode($http_response_header));
https://eval.in/145906
I have a small problem. When I fetch a page via file_get_contents, the unknown symbol is concantenated to output which destroys the XML structure.
How can I solve the problem?
code:
file_get_contents('https://emea2cps.adobeconnect.com/api/xml?action=login&login=EMAIL&password=PASSWORD');
$cookies = array();
foreach ($http_response_header as $hdr) {
if (preg_match('/^Set-Cookie:\s*([^;]+)/', $hdr, $matches)) {
parse_str($matches[1], $tmp);
$cookies += $tmp;
}
}
//print_r($cookies);
//echo "//////////////////";
$cook=$cookies['BREEZESESSION'];
echo $cook;
$opts = array(
'http'=>array(
'method'=>'GET',
'header'=>'Cookie: BREEZESESSION='.$cook.'\r\n',
)
);
$context = stream_context_create($opts);
echo "////////////////////////";
// Open the file using the HTTP headers set above
//$file = file_get_contents('http://www.example.com/', false, $context);
$file1 = file_get_contents('https://meet77842937.adobeconnect.com/api/xml?action=report-my-meetings', false, $context);
print_r($file1);
$xml = new SimpleXMLElement($file1);
output:
em2breezgwbhxopvpvknsux7////////////////////////sample1aksamaimeet77842937.adobeconnect.com/sample1/2014-02-28T06:15:00.000-08:002014-02-28T07:15:00.000-08:00true01:00:00.000sample2meet77842937.adobeconnect.com/sample2/2014-02-28T06:15:00.000-08:002014-02-28T07:15:00.000-08:00true01:00:00.000lastTtmeet77842937.adobeconnect.com/lastone/2014-02-28T15:30:00.000-08:002014-02-28T18:00:00.000-08:00false02:30:00.000�
Look at the last symbol. It must not exist ideally.
With regards