IM gettin: "Invalid argument supplied for foreach()"
in the for each line when trying to read an specific item of the xml
PHP:
$urls="http://www.floatrates.com/daily/usd.xml";
$url= $urls;
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$feed = new SimpleXMLElement($data,LIBXML_NOCDATA);
foreach ($feed->channel->item as $item) {
$title = $item->title;
if (preg_match('/ARS/',$title)) {
echo $title;
}
}
$urls = "http://www.floatrates.com/daily/usd.xml";
$url = $urls;
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$feed = new SimpleXMLElement($data, LIBXML_NOCDATA);
foreach ($feed->children() as $item) {
if ("item" == $item->getName()) {
$title = $item->title;
if (preg_match('/ARS/', $title)) {
echo $title;
}
}
}
It probably means that $feed does not contain what you expect it to contain, and you are not passing array to foreach Use print_r($feed); to see that it actually contains, and go from there.
Related
I found this code that does what I'm looking for on a single domain check. But I have a list of URLs that I would like to check.
Can you and how would you create an array with curl?
$url = 'http://www.example.com';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, true); // we want headers
curl_setopt($ch, CURLOPT_NOBODY, true); // we don't need body
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT,10);
$output = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
echo 'HTTP code: ' . $httpcode;
If all you want are headers, then instead of curl try:
$urls = array('http://www.example.com','http://www.example.com2','http://www.example.com3','http://www.example4.com');
$httpCodes = [];
foreach($urls as $url)
{
$httpCodes[$url] = get_headers($url)[0];
}
var_dump($httpCodes);
This uses get_headers() to get the header data from a URL's response.
Edit: Or if you really want to use curl. you can use the following code with curl_multi instead:
$urls = array('http://www.example.com','http://www.example.com2','http://www.example.com3','http://www.example4.com');
$curlMultiReq = curl_multi_init();
$ch = [];
foreach($urls as $key => $url)
{
$ch[$key] = curl_init($url);
curl_setopt($ch[$key], CURLOPT_HEADER, true); // we want headers
curl_setopt($ch[$key], CURLOPT_NOBODY, true); // we don't need body
curl_setopt($ch[$key], CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch[$key], CURLOPT_TIMEOUT,10);
curl_multi_add_handle($curlMultiReq, $ch[$key]);
}
$running = '';
do {
curl_multi_exec($curlMultiReq, $running);
curl_multi_select($curlMultiReq);
} while ($running > 0);
foreach(array_keys($ch) as $key){
echo curl_getinfo($ch[$key], CURLINFO_HTTP_CODE);
echo "\n";
curl_multi_remove_handle($curlMultiReq, $ch[$key]);
}
curl_multi_close($curlMultiReq);
I want to display http://services.xyz.com/tours.asmx/Sample?type=2&CityID=1146&days=4 this array data in HTML.
Here is my code:
ini_set("display_errors", 1);
$url = "http://services.xyz.com/tours.asmx/Sample?type=2&CityID=1146&days=4";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
//curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); // set the fields to post
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // make sure we get the response back
$xml = curl_exec($ch); // execute the post
curl_close($ch); // close our session
include "XMLParser.php";
$parser = new XMLParser();
$tour = $parser->parseString($xml);
print_r($tour);
//$errors = array_filter($tour);
if (empty($tour)) {
echo 'Error';
}else{
echo 'No Error';
}
//print_r($xml);
//var_dump( $xml);
//echo $xml;
//$xmlfile = file_get_contents($tour);
$ob= simplexml_load_string($tour);
//$json = json_encode($ob);
$configData = json_decode($ob, true);
foreach($configData as $result){
echo '<tr>';
echo '<td>'.$result->name.'</td>';
echo '<td>'.$result->phone.'</td>';
echo '<td>'.$result->email.'</td>';
echo '</tr>';
}
I am getting this error Warning: Invalid argument supplied for foreach() in C:
Please help me.
Thanks
Here is my final Working code to help someone else who need this code.
<?php
ini_set("display_errors", 1);
//$days = "http://services.xyz.com/tours.asmx/Days?type=2&cityID=1146";
$url = "http://services.xyz.com/tours.asmx/Sample?type=2&CityID=1146&days=4";//Replace this with your own Web services Link
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // make sure we get the response back
$xml = curl_exec($ch); // execute the post
curl_close($ch); // close our session
$var = simplexml_load_string($xml);
$configData = json_decode($var, true);
/*echo '<pre>';
print_r($configData);
echo '</pre>';*/
foreach ($configData as $row) {
echo 'Day:' . $row['Day'].'<br>';
echo 'Time:' .$row['Time'].'<br>';
echo 'Description:' .$row['Description'].'<br>';
}?>
How can I get all url-s from https://api.tenor.com/v1/trending?key=LIVDSRZULELA&limit=8 (results->media->nanomp4->url)
if($json = cURLGetContents("https://api.tenor.com/v1/trending?key=LIVDSRZULELA&limit=8")) {
$obj = json_decode($json);
echo $obj->results->{"media"}->{"nanomp4"}->{"url"};
}
Function cURLGetContents($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
On a circulate basis:
$json = cURLGetContents("https://api.tenor.com/v1/trending?key=LIVDSRZULELA&limit=8");
$urlList = getAllUrls((array) json_decode($json, true));
function getAllUrls($input, $list = []) {
foreach ($input as $key => $data) {
if (is_array($data)) {
$list = getAllUrls($data, $list);
} elseif ($key === 'url') {
$list[] = $data;
}
}
return $list;
}
$endereco = "https://api.tenor.com/v1/search?key=MBDPHCT6LA4H&q=sexo&limit=2";
$GrabURL = cURLGetContents($endereco);
$searchResponse = json_decode($GrabURL, true);
foreach ($searchResponse["results"] as $searchResult) {
print_r($searchResult["media"][0]["tinygif"]["url"]);
}
function cURLGetContents($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
I am a beginner, in php, and i want to use regex to remove a string after the first white space i found.
My code is like:
<?php
$barcode = $_POST['barcode'];
$adress = "https://...";
$timeout = 40;
$ch = curl_init($adress);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
if (strpos($adress, 'https://') === 0) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
}
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$page_content = curl_exec($ch);
$dom = new \DOMDocument('1.0', 'UTF-8');
$internalErrors = libxml_use_internal_errors(true);
$node = $dom->loadHTML($page_content);
$listeImages = $dom->getElementsByTagName('img');
foreach ($listeImages as $image)
{
$link = $dom->createElement("a");
$href = $dom->createAttribute('srcset');
$href->value = $image->getAttribute('srcset');
$value_img = print( $href -> nodeValue.'<br />');
$image->parentNode->replaceChild($link, $image);
$link->appendChild($href);
/*$exclusion_space_url=array(' ', ' 1x');
$url_cleaned=$this->$value_img;
foreach ($exclusion_space_url as $key=>$value) {
$url_cleaned=str_replace($value," ",$url_cleaned);*/ ***what i tried to do***
}
}
curl_close($ch);
?>
And the result of this is :
An html page with 3-4 lines of url serparate with space
Thank for your help
$yourString = "Your String With Space";
$str= strtok($yourString,' ');
echo $str;
When i am Decoding using commented "$jsonString" String it is working very well.
But after using curl it is not working, showing Null.
Please Help Me in this.
if (isset($_POST['dkno'])) {
$dcktNo = $_POST['dkno'];
$url = 'http://ExampleStatus.php?dkno=' . $dcktNo;
$myvars = '';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $myvars);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$jsonString = curl_exec($ch);
// $jsonString = '[{"branchname":"BHUBNESHWAR","consignee":"ICICI BANK LTD","currentstatus":"Delivered by : BHUBNESHWAR On - 25/07/2015 01:00","dlyflag":"Y","PODuploaded":"Not Uploaded"}]';
if ($jsonString != '') {
$json = str_replace(array('[', ']'), '', $jsonString);
echo $json;
$obj = json_decode($json);
if (is_null($obj)) {
die("<br/>Invalid JSON, don't need to keep on working on it");
} else {
$podStatus = $obj->PODuploaded;
}
}
}
}
After curl I used following concept to get only JSON data from HTML Page.
1) fetchData.php
$url = 'http://DocketStatusApp.aspx?dkno=' . $dcktNo;
$myvars = '';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $myvars);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$jsonString = curl_exec($ch);
// now get only value
$dom = new DOMDocument();
$dom->loadHTML($jsonString);
$thediv = $dom->getElementById('Label1');
echo $thediv->textContent;
2) JSONprocess.php
if (isset($_POST['dkno'])) {
$dcktNo = $_POST['dkno'];
ob_start(); // begin collecting output
include_once 'fetchData.php';
$result = ob_get_clean(); // Completed collecting output
// Now it will show & take only JSON Data from Div Tag
$json = str_replace(array('[', ']'), '', $result);
$obj = json_decode($json);
if (is_null($obj)) {
die("<br/>Invalid JSON, don't need to keep on working on it");
} else {
$podStatus = $obj->PODuploaded;
}
}