I have the following php function:
function checkJQL($filter) {
$auth = "account:password";
$URL='http://jira/rest/api/latest/search?jql='.$filter;
echo $URL;
// Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//Tell it to always go to the link
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$URL);
//Set username and password
curl_setopt($ch, CURLOPT_USERPWD, $auth);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);
echo $result;
// Will dump a beauty json :3
var_dump(json_decode($result, true));
echo "DONE";
}
When I call this with $filter = "" It works fine, outputs everything. As soon as I insert a proper JQL query, it fails. When I enter random garbage, it works (As in I get a invalid input message), but when it's proper JQL it never works.
When I copy and paste the URL that I echo into the browser it works.
An example filter I used:
"Target" = "Blah"
When I think about it, I don't actually need this to work, I just need it to know when the input isn't JQL (which it does). But I'm really curious now. Anyone have ideas on what it might be?
You should URL-encode the $filter.
The reason why it works with empty or random strings is because they don't have any challenging characters that need to be URL-encoded.
The reason why the URL works in the browser but not in the script is because the browser does the URL encoding.
So do the following:
$URL='http://jira/rest/api/latest/search?jql='.urlencode($filter);
Link to urlencode: http://php.net/manual/en/function.urlencode.php
Related
I have two PHP sites, an API site (x.php) and another, which calls the API site with CURL (y.php)
The x.php looks like this:
if (isset($_POST['testconnection'])) {
return "ok";
}
And the y.php like this:
$host = "https://there.is.the/x.php";
$command = array (
"testconnection" => "testconnection"
);
$ch=curl_init($host);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($command));
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT,180);
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
As you can see in the example, I would like to get the return string from x.php in the y.php, but I get an empty string in the answer: string(0) ""
I think it would be: string(2) "ok"
I have replaced the return to echo in x.php but without success.
Sorry if it is a noob question, I'm quite new in curl.
Replace
return "ok";
With
echo "ok";
Your script is exiting with return value, but nothing is output as a response.
A thing to note is that web server must have permissions to access the x.php. Verify that the script is able to execute. Permission problems often result in a blank page.
Tip: Use Postman to test REST APIs. You could shoot the post query against your x.php and see what comes back, eliminating the curl part being wrong.
I am using an API to get the users profile picture. The call looks something like this
https://familysearch.org/platform/tree/persons/{$rid}/portrait?access_token={$_SESSION['fs-session']}&default=https://eternalreminder.com/dev/graphics/{$default_gender}_default.svg
This link only works for about an hour because the user's session token expires then. I was wondering if there was any way to retrieve the last returned returned URL, which would be the direct link to the image, so I could store that in a database.
I have tried Google but I don't really know where to start.
Thanks in advance!
I was able to solve my own problem. It was doing a redirect to get the image and I just needed that URL. Here is my code that helped me get there.
$url="http://libero-news.it.feedsportal.com/c/34068/f/618095/s/2e34796f/l/0L0Sliberoquotidiano0Bit0Cnews0C12735670CI0Esaggi0Eper0Ele0Eriforme0Ecostituzionali0EChiaccherano0Ee0Eascoltano0Bhtml/story01.htm";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Must be set to true so that PHP follows any "Location:" header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$a = curl_exec($ch); // $a will contain all headers
$url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); // This is what you need, it will return you the last effective URL
// Uncomment to see all headers
/*
echo "<pre>";
print_r($a);echo"<br>";
echo "</pre>";
*/
echo $url; // Voila
:)
I'm trying to send an XML using curl but not as post parameter. what I mean is this.
for example.
the receiving side of that XML won't be able to recieve the XML using $_POST variable.
he will need to use the following code:
$xmlStr=null;
$file=fopen('php://input','r');
$xmlStr=fgets($file);
I want to be able to send an xml string using curl via https.
so the following would be wrong:
public static function HttpsNoVerify($url,$postFields=null,$verbose=false) {
// Initialize session and set URL.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
// Set so curl_exec returns the result instead of outputting it.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if ($postFields !=null) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
}
if ($verbose) {
curl_setopt($ch, CURLOPT_VERBOSE, 1);
}
// Get the response and close the channel.
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
because here i can use HttpsNoVerify($url,array('xml_file'=>'xml..')); and that
will paste it as post parameter. and i want it as post output.
so please I hope i explained myself properly and I explained exactly what I don't want to do.
how can I do what i want to do?
thanks! :)
kfir
Just directly pass the xml string as second parameter instead of an associative array item,
HttpsNoVerify($url, 'xml ..');
This will eventually call
curl_setopt($ch, CURLOPT_POSTFIELDS, "xml ...");
Which will be put in php://input for the remote server.
I am trying to take a list of URL's from a textbox, it has 1 URL per line and each URL does a redirect, I am trying to get the URL that it redirects to.
When I run this code below on a single URL, it returns the redirected URL which is what I want...
function getRedirect($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($ch);
$info = curl_getinfo($ch); //Some information on the fetch
curl_close($ch);
echo '<pre>';
print_r($info);
echo '</pre>';
}
$url = 'http://www.domain.com/go?a:aHR0cDovL2xldGl0Yml0Lm5ldC9kb3d';
getRedirect($url);
Now my problem is when I try to run it on multiple URL's with this code...
if(isset($_POST['urls'])){
$rawUrls = explode("\n", $_POST['urls']);
foreach ($rawUrls as $url) {
getRedirect($url);
}
}
When I run it on my list of URL's instead of giving me the redirected URL like my first example does correctly, it instead gives me the URL that I passed into cURL.
Can someone help me figure out why or how to fix this please?
It's already covered in the question comments but it seems the problem would be extra spacing at the end of the url.
Calling getRedirect(trim($url)) would fix it.
The space at the end is most likely turned into a querystring space (aka %20) and changes the value of query string parameters
Can someone please tell me where is the error in this code?, I use a mobile iphone application to call a php script tha will send information to apple. Apple then will return a JSON object containing several values in an associative array.
I want to reach the 'status' value but every time I run the code in the phone, the php script sends me the complete apple's returned string. In the XCode debugger the received string looks like:
[DEBUG]... responseString :
{"receipt":{"item_id":"328348691",
"original_transaction_id":"1000000000081203",
"bvrs":"1.0", "product_id":"julia_01",
"purchase_date":"2009-10-05 23:47:00
Etc/GMT", "quantity":"1",
"bid":"com.latin3g.chicasexy1",
"original_purchase_date":"2009-10-05
23:47:00 Etc/GMT",
"transaction_id":"1000000000081203"},
"status":0}
but the only piece I care in the string is the "status" value.
I've already looked inside documentation but can't find the solution. I'm new in php but this getting too long. Here the script:
<?php
//The script takes arguments from phone's GET call
$receipt = json_encode(array("receipt-data" => $_GET["receipt"]));
//Apple's url
$url = "https://sandbox.itunes.apple.com/verifyReceipt";
//USe cURL to create a post request
//initialize cURL
$ch = curl_init();
// set the target url
curl_setopt($ch, CURLOPT_URL,$url);
// howmany parameter to post
curl_setopt($ch, CURLOPT_POST, 1);
// the receipt as parameter
curl_setopt($ch, CURLOPT_POSTFIELDS,$receipt);
$result = curl_exec ($ch);
//Here the code "breaks" and return the complete string (i've tested that)
//and apparently doesn't get to the json_decode function (i think something's wrong there, so code breaks here)
curl_close ($ch);
$response = json_decode($result);
echo $response->{'status'};
?>
Even if I don't put any echo at the end, the script still returns a complete string (odd to me)
Thank's in advance and apollogies if I insist again from another question
Try setting the RETURNTRANSFER option to 1 so you can capture the output from the requested URL as a string. It seems that the default behaviour of cURL is to output the result directly to the browser:
...
$ch = curl_init();
// set the target url
curl_setopt($ch, CURLOPT_URL,$url);
// howmany parameter to post
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // <---- Add this
// the receipt as parameter
curl_setopt($ch, CURLOPT_POSTFIELDS,$receipt);
...