This question already has answers here:
PHP Regex replace text between brackets AND brackets
(3 answers)
Closed 28 days ago.
this is my curl array
CURLOPT_URL => 'https://xxxxxx.com/v2/transaction?ref_id=[ref_id]',
and i need to change [ref_id] to $result['useridInvoice']
what is the best url to solve this problem
help me please to solve my problem
$result['useridInvoice'] to create the final URL.
For example, you can use the following line of code to create the final URL:
$url = 'https://xxxxxx.com/v2/transaction?ref_id=' . $result['useridInvoice'];
Then you can set the value of CURLOPT_URL to this variable:
curl_setopt($curl, CURLOPT_URL, $url);
You can also use the sprintf function to format the URL string and enter the value of$result['useridInvoice']:
$url = sprintf('https://xxxxxx.com/v2/transaction?ref_id=%s', $result['useridInvoice']);
curl_setopt($curl, CURLOPT_URL, $url);
Both of these methods will replace the [ref_id] placeholder in the URL with the value of $result['useridInvoice'], resulting in the desired final URL.
Related
This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 2 years ago.
The following PHP code is working fine passing a specific IP Address to curl_init:
$ch = curl_init('https://ipgeolocation.abstractapi.com/v1/?api_key=my_key&ip_address=5.79.66.162');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$yummy = json_decode($data);
$country= $yummy->country; //this is working fine. country is being passed
===========================================
But I need to pass the ip address in a variable in the curl_init. However you cannot pass a PHP variable between single quotes. So the following curl_init statement is NOT working.
$ip="5.79.66.162";
$ch = curl_init('https://ipgeolocation.abstractapi.com/v1/?api_key=my_key&ip_address=$ip');
How does the code above = need to be changed in order for the call to work using a variable?
I would appreciate any help anyone can offer.
However you cannot pass a PHP variable between single quotes
Exactly, so you can use doble quotes here or string concatenation:
$ip="5.79.66.162";
$ch = curl_init("https://ipgeolocation.abstractapi.com/v1/?api_key=my_key&ip_address=$ip");
or
$ip="5.79.66.162";
$ch = curl_init('https://ipgeolocation.abstractapi.com/v1/?api_key=my_key&ip_address=' . $ip);
This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 5 years ago.
So, I want to make my site check the bitcoin prices. I do that with virwox. So, I send a curl to http://api.virwox.com/api/json.php?method=getBestPrices&symbols[0]=BTC/SLL.
That gives me this:
{"result":[{"symbol":"BTC\/SLL","errorCode":"OK","bestBuyPrice":"983021","bestSellPrice":"1009989"}],"error":null,"id":""}
Now, I want the bestBuyPrice. So, I runned a json_decode on my $output. Then my $output is an array. So, I did json_encode("$output["result"]") to see what will come out of that. It gave me this:
[{"symbol":"BTC\/SLL","errorCode":"OK","bestBuyPrice":"983021","bestSellPrice":"1009989"}]
So, I tought if I do json_decode("$output["bestBuyPrice"]") I would get it, but I didn't. What I got was: null. How can I fix this?
Here is my full code:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://api.virwox.com/api/json.php?method=getBestPrices&symbols[0]=BTC/SLL");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
$decode = json_decode($output, true);
$encode = json_encode($decode["result"]["bestBuyPrice"], true);
echo $encode; // Gives null
$koop = $decode["result"]["bestBuyPrice"]; // No result
$verkoop = $decode["result"]["bestSellPrice"]; // No result
echo $koop . "<br />".$verkoop;
?>
Thanks!
This one's real easy. Get rid of the quotes from around your variable!
json_encode("$output["result"]")
to
json_encode($output["result"])
EDIT:
Based on the var_dump in the comment below, it looks like there is another array key you need:
$koop = $decode["result"][0]["bestBuyPrice"];
Give that a try!
I am trying to update my API with an update curl function but am struggling to work out why it isn't working
The areas where it may be wrong is key($id) I want it to
extract the ID column based on the key value for the ID array.
$URL I want to create the URL based on the const variables plus the resource name plus the value of the ID array that has been passed through rawurlencode.
So far this is my update code, but am wondering what area is wrong.
I can provide more info if needed and appreciate any help, thanks
<?php
function update(array $id,array $vaules, $resourcename)
$jsonData = json_encode($vaules);
key($id);
$url = DOMAIN.FOLDER.APIPATH.$resourcename.rawurlencode("/".$id);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,array ('content-type: application/json'));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,PUT);
curl_setopt($ch,CURLOPT_POSTFIELDS,$jsonData);
curl_exec($ch);
curl_getinfo(CURLINFO_HTTP_CODE);
}
The function key() returns the current key in an array (according to the internet pointer). Right now you're not doing anything with it, you're calling the function and not assigning it anywhere.
Did you mean to write: rawurlencode("/".key($id).$vaules);?
As your code is right now, assuming $id is an array, you're trying to convert an array into a string, which I doubt is what you want.
This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 8 years ago.
I want to output the third element from the same HTML element. I know how to output a div or span with an id or a class but I have no idea how to output a same HTML element. I thought it was something with p[1] but it doesn't work.
I know there is a lot of answered questions about it but it never explained how to output the same HTML element without a class or id.
website : http://localhost/
<p>example</p>
<p>example1</p> <!-- i want to take this one -->
<p>example2</p>
-------------------
php script :
<?php $curl = curl_init('http://localhost/');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$code3 = curl_exec($curl);
curl_close($curl);
$code = '/<p>(.*?)<\/p>/s';
$code6= preg_match($code, $code3, $code4);
echo $code4[1]
?>
/* doesn't work ..
also php.net doesnt give a good example about it so i hope someone can help me here.
thanks advanced !
*/
Try:
<?php
$curl = curl_init('http://justpaste.it/8s5v');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$content = curl_exec($curl);
curl_close($curl);
preg_match_all('/<p>(.+)<\/p>/im', $content, $result);
print_r($result);
print "Selected: " . $result[0][1] . "\n";
?>
Im wondering if there is an easy way to access the information in a huge string i have, the string is structured, for the purpose of people reviewing it i put line breaks and space but this is just one huge single line of text that's returned:
First this is how i access the Jira API:
$username = 'xxx';
$password = 'xxx';
$url = 'https://xxx.atlassian.net/rest/api/2/Issue/Bug-5555';
$curl = curl_init();
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
$issue_list = (curl_exec($curl));
echo $issue_list;
Now that returns a huge string which when broken down looks like this:
{"expand":
"renderedFields,names,schema,transitions,operations,editmeta,changelog",
"id":"16935",
"self":"https://xxx.atlassian.net/rest/api/2/issue/16935",
"key":"xx-5555",
"fields":
{"summary":"Dialog boxes shouldn't be on top.",
"progress":
{"progress":0,
"total":0
},
"timetracking":{},
"issuetype":
{"self":"https://xxx.atlassian.net/rest/api/2/issuetype/1",
"id":"1",
"description":"A problem which impairs or prevents the functions of the product.",
"iconUrl":"https://xxx.atlassian.net/images/icons/bug.gif",
"name":"Bug",
"subtask":false
},
"timespent":null,
"reporter":
{"self":"https://xxx.atlassian.net/rest/api/2/user?username=xxx%40xxx.com",
"name":"xxx#xx.com",
"emailAddress":"xxx#xxx.com",
"avatarUrls":{"16x16":"https://xxx.atlassian.net/secure/useravatar?size=small&avatarId=10122",
"48x48":"https://xxx.atlassian.net/secure/useravatar?avatarId=10122"},
"displayName":"xxx",
"active":true
},
"created":"2012-08-25T18:39:27.760-0600",
"updated":"2012-08-31T16:47:38.761-0600",
"priority":
{"self":"https://xxx.atlassian.net/rest/api/2/priority/6",
"iconUrl":"http://dl.dropbox.com/u/xxx/3-green.png",
"name":"3 - Medium Priority",
"id":"6"
},
"description":"\"loading \" dialog is always on top, so is the \"Updating database\" dialog.\r\n\r\n\r\nThis is annoying. It shouldn't be on top and/or you should be able to easily minimize the window.",
"issuelinks":[], etc etc etc
Now im a basic php user so please try and keep the replies simplistic if possible, before i go down the route of parsing the whole document which will be difficult for me as im not familiar with parsing i was wondering if there was an easy way to access the values.
what im thinking is something like this:
foreach($issue_list->issues as $issue) {
echo "summary" . $issue->summary;
echo "updated" . $issue->updated;
echo "created" . $issue->created;
echo "description" . $issue->description;
}
Now this might be wishful thinking but i seen an article where i guy done something similar but i cant figure it out, here is the article:
http://www.lornajane.net/posts/2012/using-jiras-rest-api-to-create-a-dashboard
Also if it is possible, how would i access the reporter > displayName value since that's 2 indents deep, would it be $issue->reporter->displayName;
Finally one quick other question, if im echoing the description, how do i get it to obey the /r/r/r/r/r/n and /" so it prints it out with line breaks and removes those special characters?
This looks like a JSON (JavaScript Object Notation - more info here) string, you could probably use json_decode (documented here) to convert it into a PHP object and then easily index it.
I don't have your full string but you can probably try something along the lines of:
$jiraIssue = json_decode($theString);
echo $jiraIssue["id"];
Now, since objects are contained inside of objects, you'll probably have to go through "fields" before you can access "summary".
You can pass true as a second parameter if you'd prefer to deal with arrays instead of objects.