I've create a script that will delete all user properties for a particular individual. I'm able to use an api call to get the users' properties from the xml. And I'm using a delete api to remove each property.
I would like to be able to test when there are no more properties left and then output a message accordingly. Inside the for loop is where each property is found. Any help is certainly appreciated.
Below is the code:
<?php
$user_id="john_smith#ourwiki.com";
$url=('http://user:12345#192.168.245.133/#api/users/=john_smith#ourwiki.com/properties');
$xmlString=file_get_contents($url);
$delete = "http://user:12345#192.168.245.133/#api/DELETE:users/$user_id/properties/%s";
$xml = new SimpleXMLElement($xmlString);
function curl_fetch($url,$username,$password,$method='DELETE')
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // returns output as a string instead of echoing it
curl_setopt($ch,CURLOPT_USERPWD,"$username:$password"); // if your server requires basic auth do this
return curl_exec($ch);
}
foreach($xml->property as $property) {
$name = $property['name']; // the name is stored in the attribute
curl_fetch(sprintf($delete, $name),'user','12345');
}
?>
The foreach construct automatically loops until the end of the enumerable object (namely $xml).
I think you're looking for the count function:
if(!count($xml->property)) die('No more properties');
Related
I'm new to Laravel. Trying to work with external API.
In controller i wont just to make variable with json:
$testapi = new TestApi();
$data['books'] = $testapi->books();
return view('books.index', $data);
In model (just php class without any extends) this:
public function books()
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $this->host."/books");
curl_setopt($curl, CURLOPT_HTTPHEADER, $this->headers);
$result = curl_exec($curl);
$result = json_decode($result);
curl_close($curl);
return $result;
}
But json content is placed at the beggining of HTML file, even before META information. curl_exec don't save result to variable, but transfer it to global result. How to save this json to variable and then use inside template?
I believe you're missing
curl_setopt($curl, CURLOPT_RETURNTRANSFER,TRUE);
that will allow the return to be put into your $result variable, instead of outputting directly.
I am working with a API. the link shows in the class like this
private $apiPath = "http://www.website.com/api/v1/users/username";
now if the username part is tom and i put it in the browser like so.
private $apiPath = "http://www.website.com/api/v1/users/tom";
it will display all toms stats in text format.
I am trying to figure out how to display it in HTML rendered format
using php so i can display it on my site. I was told its in an array.
so I am assuming using variables i can get it to display what and where I want. I am just really unclear.
I have the class but unsure what to do. do i include it?
This will allow you to grab the username from db and compare
function stats(){
$user_id = $_GET['uid'];
$sql = "SELECT * FROM users WHERE uid = $user_id ";
$result = query($sql);
$row = mysqli_fetch_object($result);
$username = $row->username);
$url = ('"http://www.website.com/api/v1/users/'.$username);
$rCURL = curl_init();
curl_setopt($rCURL, CURLOPT_URL, $url);
curl_setopt($rCURL, CURLOPT_HEADER, 0);
curl_setopt($rCURL, CURLOPT_RETURNTRANSFER, 1);
$aData = curl_exec($rCURL);
curl_close($rCURL);
$response = json_decode($aData, true);
}
If you just want the data that the API is returning, that class would tell me that it is returning JSON which is a data interchange format. You could use PHP's cURL function to get the data from the API and then decode it into a PHP array using json_decode and then you could do what you like with the data.
For example:
$apiUrl = "http://www.website.com/api/v1/users/$username";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$apiURL);
$result=curl_exec($ch);
curl_close($ch);
$userArray = json_decode($result, true);
If you need to use the class as you need to process the data into the form it does, you will need to include the class and use it.
import 'myclass.php';
$userData = new statsProfile('name');
Then you can use the other methods of the class as you need.
Not sure if the title is helpful.. but i'll break it down.
Basically I need to perform a curl delete against multiple objects in an API call in PHP.
To do the delete, I generally put the object name at the end of the API request URL
$request = "/objects/[insert object name here]"
which is then authenticated and is deleted via CURL
$signed_request = [authentication stuff goes here]
then-
$put_response = do_delete($signed_request);
function do_delete($url, $params=array()) {
$session = curl_init($url);
curl_setopt($session, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_POSTFIELDS, $params);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($session);
curl_close($session);
return $response;
}
I have about 11,000 objects that I need to put into the "[insert object name here]" part. I have the list of objects in a text file, each object on a new line. apologies for being so vague.
The file function will make it easy. It reads a file and split the lines into an array :
$my_objects = file ('my_file_with_objects.txt', FILE_IGNORE_NEW_LINES);
foreach ($my_objects as $object) {
$str = "....." . $object; // $object being your "[insert object name here]" part
}
Read the documentation to see more examples on file
As suggested by #fejese, I added the FILE_IGNORE_NEW_LINES flag to escape end lines from the result.
This is probably a stupid question, but I'm just wondering if this is possible or if I'm supposed to do something else...
When using multi-curl one would use URLs right?
// create both cURL resources
$ch1 = curl_init();
$ch2 = curl_init();
// set URL and other appropriate options
curl_setopt($ch1, CURLOPT_URL, "http://lxr.php.net/");
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_setopt($ch2, CURLOPT_URL, "http://www.php.net/");
curl_setopt($ch2, CURLOPT_HEADER, 0);
etc..
Per the multi-curl document...
So what if I have some method (I think thats what you call it) that I'm using from a library
$tags = $instagram->searchTags( 'tag' );
Now thats searching the library of the word tag. But what if I want to be able to do multiple searches,
$tags1 = $instagram->searchTags( 'tag' );
$tags2 = $instagram->searchTags( 'tagme' );
How do I implement this into multi curl? Is it just simply replacing the URLs with $tags1 and tags2?
This is without your class, dont see why you need that.
function fetchHTML($website) {
if(function_exists('curl_init')) {
$ch = curl_init($website);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
$content = curl_exec($ch);
curl_close($ch);
} else {
$content = file_get_contents($website)
}
return $content;
}
$dom = new DOMDocument();
$dom->loadHTML(fetchHTML("http://example1.com"));
$tag1 = $dom->getElementsByTagName('tagname');
$dom->loadHTML(fetchHTML("http://example2.com"));
$tag2 = $dom->getElementsByTagName('tagname');
/* Will give you a DOM object list with your first tagname */
print_r($tag1);
/* Will give you a DOM object list with your second tagname */
print_r($tag2);
I have looked into that PHP library anh found that each instance of the Instagram class use only one cURL handler, which leads to the fact that you cannot send multiple request asynchronously.
You can read this article about connection Sharing with CURL in PHP to get the idea of modify the CurlClient class of the Instagram library. The main idea here is keep a static class member which hold a handler from curl_multi_init() and add each new cURL single handler to it when you need.
I've been working on a script that can be used for an internal wiki, that will remove the properties for a user who is inactive. I believe I'm almost there but having a problem with the api.
I think I need to use urlencode on the $delete path BUT only for the # in the email address and # in the property. I know how to use urlencode for the whole thing but not on just that. The way it works, is it loops through to get the properties and most of them include # in the name. Anyone who can help modify so that this works would be greatly appreciated!
Here is the script:
<?php
$user_id="john_smith#ourwiki.com";
$url=('http://admin:12345#192.168.245.133/#api/deki/users/=john_smith#ourwiki.com/properties');
$xmlString=file_get_contents($url);
$delete = "http://admin:12345#192.168.245.133/#api/deki/DELETE:users/$user_id/properties/%s";
$xml = new SimpleXMLElement($xmlString);
function curl_fetch($url,$username,$password,$method='DELETE')
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // returns output as a string instead of echoing it
curl_setopt($ch,CURLOPT_USERPWD,"$username:$password"); // if your server requires basic auth do this
return curl_exec($ch);
}
foreach($xml->property as $property) {
$name = $property['name']; // the name is stored in the attribute
curl_fetch(sprintf($delete, $name),'admin','12345');
}
?>
Like this?
$delete = "http://admin:12345#192.168.245.133/#api/deki/DELETE:users/".urlencode($user_id)."/properties/%s";