If I go directly to the URL with the right params in the URL, I get a success response.
The URL looks like
https://api.theService.com/send?client_id=54&token=7545h5h554&format=json
In PHP, I tried something like
error_reporting(E_ALL);
ini_set('display_errors', 1);
$client_id = 54;
$token = '7545h5h554';
$ch = curl_init('https://api.theService.com/send?client_id='.$client_id.'&token='.$token.'&format=json');
// Send the request
$response = curl_exec($ch);
// Check for errors
if($response === FALSE){
die(curl_error($ch));
}
// Decode the response
$responseData = json_decode($response, TRUE);
echo "<pre>";
echo print_r($responseData);
echo "</pre>";
However I never get a response with the above code.
Try this:
$client_id = 54;
$token = '7545h5h554';
$url = "https://api.theService.com/send?client_id=$client_id&token=$token&format=json";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSLVERSION, 3);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); //waits 10 seconds for response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$page = curl_exec($ch) or die(curl_error($ch));
echo $page;
You should also try checking the error messages in curl_error().
http://www.php.net/curl_error
Related
I'm using the below curl request which returns a XML structured response. The response however seems wrong (first line: string(744978) "<?xml etc). I normally json_decode it, however that does not seem to work. Is this an issue in the endpoint or am I doing something wrong? I would like to convert the response to an array so I can store it in a database.
REQUEST
$url = 'url';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
RESPONSE
use this function for result like
$url = 'url';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
$result = api_result($result);
curl_close($ch);
function api_result($result){
$plainXML = $this->mungXML($result);
$arrayResult = json_decode(json_encode(SimpleXML_Load_String($plainXML, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
return $arrayResult;
}
<!-- begin snippet: js hide: false console: true babel: false -->
function xml_to_json_feed() {
$url = 'http://x';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
$xml = simplexml_load_string($result);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
curl_close($ch);
return $array;
}
I have a link: http://rstyle.me/n/bfj42hqgww
which redirects to https://www.missguided.co.uk/clothing/coats-jackets/double-breasted-tailored-long-faux-wool-coat-camel
How do I get the redirect link?
I tried the following:
$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);
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
echo $url;
but it did not work. I also tried:
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE); // We'll parse redirect url from header.
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE); // We want to just get redirect url but not to follow it.
$response = curl_exec($ch);
preg_match_all('/^Location:(.*)$/mi', $response, $matches);
curl_close($ch);
echo !empty($matches[1]) ? trim($matches[1][0]) : 'No redirect found';
i don't have any knowledge about this but i am using in my website something like this for sending sms. so, you could try if it is workout.
$URL="example.com";
$ch = curl_init($URL);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
You got redirect not from server but from javascript. cURL doesn't execute javascript. When I open your link I saw that real link is hidden in a "title" tag.
$url = 'http://rstyle.me/n/bfj42hqgww';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
$data = curl_exec($curl);
curl_close($curl);
// Get real URL.
preg_match("|<title>(.+?)</title>|si", $data, $matches);
$link = ( isset($matches[1]) ) ? $matches[1] : '';
echo $link;
Here is the url below, tried both with the method file_get_contents and curl...doesn't work from php script but works fine with postman.
Any idea why so???
https://query.yahooapis.com/v1/public/yql?q= select * from weather.forecast where woeid in (SELECT woeid FROM geo.places WHERE text="(28.56,77.32)") and u='c'&format=json
cURL request is as follows:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$output = curl_exec($ch);
// convert response
$output = json_decode($output);
// handle error; error output
if(curl_getinfo($ch, CURLINFO_HTTP_CODE) !== 200) {
var_dump($output);
}
echo curl_error($ch);
curl_close($ch);
Try with this one. The difference is that the url is url encoded:
$url = 'https://query.yahooapis.com/v1/public/yql?q=%20select%20*%20from%20weather.forecast%20where%20woeid%20in%20(SELECT%20woeid%20FROM%20geo.places%20WHERE%20text=%22(28.56,77.32)%22)%20and%20u=%27c%27&format=json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
$output = curl_exec($ch);
I have a Rest api which I can access by this url: "http://127.0.0.1:8000/api/thesis/?format=json". Now I want to get the JSON data from it. For connecting to the api I tried to use PHP-Curl as below. But I get NULL! (This is the first time I'm doing php any help will be great!)
<?php
$service_url = "http://127.0.0.1:8000/api/thesis/?format=json";
//initialize a curl session
$curl = curl_init();
//set options for the transfer
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_TIMEOUT, 5);
curl_setopt($curl, CURLOPT_URL, $service_url);
curl_setopt($curl, CURLOPT_POST, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
//execute the session
$curl_response = curl_exec($curl);
//finish off the session
curl_close($curl);
$curl_jason = var_dump(json_decode($curl_response, true));
print_r($curl_jason);
echo $curl_jason;
?>
I think you should use GET method of curl like this
$service_url = "http://127.0.0.1:8000/api/thesis/?format=json";
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
//execute the session
$curl_response = curl_exec($curl);
//finish off the session
curl_close($curl);
$curl_jason = json_decode($curl_response, true);
print_r($curl_jason);
Use the below mentioned code snippet to fetch data from a REST API using PHP curl
<?php
function _isCurl(){
return function_exists('curl_version');
}
if (_iscurl()){
//curl is enabled
$url = "http://testDomainName/restAPI.php?id=123&amt=100&jsonp=?";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
print_r($output);
// Curl operations finished
}
else{
echo "CURL is disabled";
}
?>
I am using cURL through a PHP file to create a fogbugz case using values I will pull from the current ticket. Right now I am only trying to display the token received when a user logs into fogbugz and I am not getting any values from the response.
Here is the code I am using in my PHP file:
<?php
$user = "USERNAME";
$password = "PASSWORD";
$url = "fogbugz.spllc.local/api.asp?";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, "[$user]:[$password]");
if (false === ($output = curl_exec($ch)))
{
print "error" . curl_error($ch);
die("ERROR" . curl_error($ch));
}
else if (200 !== (int)curl_getinfo($ch, CURLINFO_HTTP_CODE))
{
print "200";
die("200");
}
print curl_exec($ch);
curl_close($ch);
?>
Can someone help point me in the right direction?
Thanks
When you read the documentation on http://fogbugz.stackexchange.com/fogbugz-xml-api you will find you have to send a get or post request.
Something like shown below should work:
$ch = curl_init('http://fogbugz.spllc.local/api.asp?cmd=logon&email=John%20Hancock&password=BigMac');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
also take a look at: https://github.com/there4/fogbugz-php-api