I have written a PHP script which is going to loop through approx. 500 URLs and check their status:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_exec($ch);
$destURL = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
echo $destURL . " - " . $statusCode;
Could this be optimized any further OR is there a better way of doing this?
You could do this quite easily with GuzzleHttp:
use GuzzleHttp\TransferStats;
$effectiveURL = '';
$statusCode = 0;
$client = new GuzzleHttp\Client();
$client->get($url, [
'on_stats' => function (TransferStats $stats) use (&$effectiveURL, &$statusCode) {
$effectiveURL = (string) $stats->getEffectiveUri();
if ($stats->hasResponse()) {
$statusCode = $stats->getResponse()->getStatusCode();
}
},
]);
echo $effectiveURL . '<br>';
echo $statusCode;
Related
I want to change profile image another application curl request i am trying below code can anyone help me please
$viewer = Engine_Api::_()->user()->getViewer();
$apiData = array(
"email" => $viewer->email,
"profile_image_file" => $_FILES['Filedata']['name'],
);
$apiHost = "https://tenant.thetenantsnet.co.uk/api/api/save_profile_image";
$response = $this->callRiseAPI2($apiData,$apiHost);
private function callRiseAPI2($apiData,$apiHost){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiHost);
curl_setopt($ch, CURLOPT_POST, count($apiData));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($apiData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$jsonData = curl_exec($ch);
if (false === $jsonData) {
throw new \Exception("Error: _makeOAuthCall() - cURL error: " . curl_error($ch));
}
curl_close($ch);
//return the API response
return json_decode($jsonData);
}
As Anoxy said, you need to put in the header the Content-Type :
$viewer = Engine_Api::_()->user()->getViewer();
$apiData = array(
"email" => $viewer->email,
"profile_image_file" => $_FILES['Filedata']['name'],
);
$apiHost = "https://tenant.thetenantsnet.co.uk/api/api/save_profile_image";
$response = $this->callRiseAPI2($apiData,$apiHost);
private function callRiseAPI2($apiData,$apiHost){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiHost);
curl_setopt($ch, CURLOPT_POST, count($apiData));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($apiData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, 'Content-Type: multipart/form-data');
$jsonData = curl_exec($ch);
if (false === $jsonData) {
throw new \Exception("Error: _makeOAuthCall() - cURL error: " . curl_error($ch));
}
curl_close($ch);
//return the API response
return json_decode($jsonData);
}
I have a problem to download the slides I have available on the website www.slideshare.net, through the API I can not download all the slides that I have available at my own regardless of whether they are public or private. Can you help me?
$apikey = 'apikey';
$secret = 'secret';
$ts = time();
$hash = sha1($secret.$ts);
$postdata = '&api_key='.$apikey.'&ts='.$ts.'&hash='.$hash;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://www.slideshare.net/api/2/get_slideshow";);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$output = curl_exec($ch);
if ($output === FALSE) {
echo "cURL Error: " . curl_error($ch);
} else {
var_dump($output);
}
curl_close($ch);
All,
I have written a curl to get the details from Adcash API. Output of this API is to get the token number after login.
Below code is working good but it is not getting the token as output. It is null. Any suggestions.
<?php
try{
Echo "Executing started";
$url = "https://www.adcash.com/console/login_proxy.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERPWD, "userid:password");
$output = curl_exec($ch);
$info = curl_getinfo($ch);
var_dump($info) ;
echo $output;
if (FALSE === $output)
throw new Exception(curl_error($ch), curl_errno($ch));
Echo "Executing Completed";
curl_close($ch);
} catch(Exception $e) {
trigger_error(sprintf(
'Curl failed with error #%d: %s',
$e->getCode(), $e->getMessage()),
E_USER_ERROR);
}
?>
I have updated the code to get the subid report. please check and let me know what is the issue.
<?php
try{
Echo "Executing started";
$url = "https://www.adcash.com/console/login_proxy.php";
$ch = curl_init();
$logindata = array (
'login' => 'xxx',
'password' => 'xxx'
);
$logindata1 = http_build_query($logindata);
curl_setopt($ch, CURLOPT_POSTFIELDS, $logindata1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
$json_a=json_decode($output,true);
$token= $json_a["token"];
curl_close($ch);
$url = "https://www.adcash.com/console/login_proxy.php";
$ch = curl_init();
$logindata1 = http_build_query($logindata);
curl_setopt($ch, CURLOPT_POSTFIELDS, $logindata1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "token=". $token . "&call=get_publisher_detailed_statistics");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, true);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
var_dump($output);
curl_close($ch);
} catch(Exception $e) {
trigger_error(sprintf(
'Curl failed with error #%d: %s',
$e->getCode(), $e->getMessage()),
E_USER_ERROR);
}
?>
I'm part of Adcash IT team. First of all, thanks for using our API. The problem in your code is that you're using HTTP authentication. Our API is using POST.
This part of your code:
curl_setopt($ch, CURLOPT_USERPWD, "userid:password");
Can be replaced by:
$logindata = array (
'login' => YOUR_LOGIN_HERE,
'password' => YOUR_PASSWORD_HERE
);
$logindata = http_build_query($logindata);
curl_setopt($c, CURLOPT_POSTFIELDS, $data);
curl_setopt($c, CURLOPT_URL, 'https://www.adcash.com/console/login_proxy.php');
Let me know if it works.
When I execute below code it work fine.
$url = "https://api.mongolab.com/api/1/databases/$DB/collections/$COLLECTION?apiKey=$MONGOLAB_API_KEY";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data),
)
);
echo $response = curl_exec($ch);
$error = curl_error($ch);
echo $response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($response_code == 200)
{
$response1=json_decode($response);
echo("<pre>");print_r($response1);echo("</pre>");
}
else
{
echo "Fail";
var_dump($data);
var_dump($status);
}
above work well,it insert one record to mongolab database and return Object id but when execute below code it return null response without any error.
I have check all that thing are right but why response is null.
$url = "https://api.mongolab.com/api/1/databases/winebook-dev/collections/persons?fo=true&q={'email':'$email'}&apiKey=".MONGOLAB_API_KEY;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$data = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($status == 200)
{
$data1 = json_decode($data);
$len = count($data1);
if($len > 0 )
{
echo "This email already resistered.";
}
else
{
$url = "https://api.mongolab.com/api/1/databases/$DB/collections/$COLLECTION?apiKey=$MONGOLAB_API_KEY";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data)
)
);
$response = curl_exec($ch);
$error = curl_error($ch);
$response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($response_code == 200)
{
$response1=json_decode($response);
echo("<pre>");print_r($response1);echo("</pre>");
}
else
{
echo "Fail";
var_dump($data);
var_dump($status);
}
}
}
if any one know solution then answer this question suddenly.
thanks in adv.
I am trying to get some information using https://api.facebook.com
The following works just fine: (notice graph.facebook.com)
$q = "https://graph.facebook.com/$params".$sep."access_token=$access_token";
$response = json_decode(file_get_contents($q),true);
But
$q = "https://api.facebook.com/$params".$sep."access_token=$access_token";
$response = json_decode(file_get_contents($q),true);
Just does not work. I've tried curl, but I keep getting Method Not Implemented
Following code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$url = "https://api.facebook.com/$params".$sep."access_token=$access_token";
echo $url;
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_URL, $url);
echo curl_exec($ch);
echo curl_errno($ch);
curl_close ($ch);
Any ideas?
Such a silly mistake. I post the working example:
The call:
$status = $fb->api("method/fql.query?query=".urlencode("SELECT url FROM url_like WHERE user_id = $ID")."",$access_token);
The class:
class fb
{
public function api($params, $access_token)
{
try
{
$q = "https://api.facebook.com/$params?format=json-strings&access_token=$access_token";
$response = json_decode(file_get_contents($q),true);
if ($response->error->message != null)
throw new Exception($response->error->message);
else
return $response;
}
catch (Exception $e)
{
return ("<b>Error:</b> " . $e->getMessage());
}
}
}
For everyone who don't want to use the facebook php sdk.