Fetching xml with curl on https - php

So I made a login to site like this:
$ch = curl_init('https://emea2cps.adobeconnect.com/api/xml?action=login&login=username&password=password');
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_SSLVERSION,3);
curl_exec($ch);
$info= curl_getinfo($ch);
echo 'passed' . $info['total_time'] . ' secconds ' . $info['url'] . '------ and http-code'. $info['http_code'];
print curl_error($ch);
After I want to fetch XML of my meetings by this link https://meet77842937.adobeconnect.com/api/xml?action=report-my-meetings
I tried the following code:
$ch1 = curl_init('https://meet77842937.adobeconnect.com/api/xml?action=report-my-meetings');
curl_setopt($ch1,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch1, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch1, CURLOPT_SSLVERSION,3);
curl_setopt($ch1,CURLOPT_RETURNTRANSFER,TRUE);
$data = curl_exec($ch1);
$info1= curl_getinfo($ch1);
echo 'passed' . $info1['total_time'] . ' secconds ' . $info1['url'] . '------ and http-code'. $info1['http_code'];
print curl_error($ch1);
curl_close($ch1);
curl_close($ch);
$xml = new SimpleXMLElement($data);
print_r($xml);
What can you advise me?
Note: i can see xml when i input this link on browser
Structure:
<results>
<status code="ok"></status>
<my-meetings>
<meeting sco-id="1282590819" type="meeting" icon="meeting" permission-id="host" active-participants="0">
<meeting sco-id="1282620938" type="meeting" icon="meeting" permission-id="host" active-participants="0">
</my-meetings>
</results>
Output:
like this for ex:
sample1aksamaimeet77842937.adobeconnect.com/sample1/2014-02-28T06:15:00.000-08:002014-02-28T07:15:00.000-08:00false01:00:00.000sample2meet77842937.adobeconnect.com/sample2/2014-02-28T06:15:00.000-08:002014-02-28T07:15:00.000-08:00false01:00:00.000

Try adding this:
curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST, FALSE);
Also:
$data = curl_exec($ch1); // after this line
var_dump(htmlentities($data)); // add this one
to see what's the output. It'll give you a starting point to debug.
UPDATE
Add:
curl_setopt($ch, CURLOPT_COOKIEFILE, __DIR__.'/cookies');
curl_setopt($ch, CURLOPT_COOKIEJAR, __DIR__.'/cookies');
// repeat this for all $ch, $ch1, $ch2 and such where you need auth cookie available
to store the login information and reuse it in other requests. Your error shows your second request goes unauthenticated. So I assume first one sets a cookie for login unless you need to carry over a variable returned by the first login sequence.
AND remove the \ in password\. Your password has an extra character that should not be there!
Working code:
// Login.
$ch = curl_init('https://emea2cps.adobeconnect.com/api/xml?action=login&login=username&password=password');
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_COOKIEFILE, __DIR__.'/cookies');
curl_setopt($ch,CURLOPT_COOKIEJAR, __DIR__.'/cookies');
$data = curl_exec($ch);
var_dump($data);
curl_close($ch);
// Query.
$ch = curl_init('https://meet77842937.adobeconnect.com/api/xml?action=report-my-meetings');
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_COOKIEFILE, __DIR__.'/cookies');
curl_setopt($ch,CURLOPT_COOKIEJAR, __DIR__.'/cookies');
$data = curl_exec($ch);
var_dump($data);
curl_close($ch);
Read it. It's clear enough.

Related

Pipedrive - Updating a Lead Returns 404 not found

I am trying to update a LEAD using this URL
$lead_url = ‘https://’.$company_domain.’.pipedrive.com/api/v1/leads/’ . $leadID . ‘?api_token=’ . $PD_API_KEY;
But it returns 404 Not Found. When I check this URL in the browser it returns the complete information of that lead.
Here is my Curl Code:
function pipedrive_update_curl($arr , $endpoint)
{
$response = array();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($arr));
//***//
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
//**//
$response_result = curl_exec($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curl_errors = curl_error($ch);
curl_close($ch);
$response['error'] = $curl_errors;
$response['status'] = $status_code;
$response['response'] = $response_result;
return $response;
}
Can somebody please explain where I am going wrong?
PUT method for updating leads is not supported.
see docs and pipedrive community posts:
https://developers.pipedrive.com/docs/api/v1/Leads#updateLead
https://devcommunity.pipedrive.com/t/put-not-working-when-updating-a-lead/3629

Walmart API Search Products

I am trying to get search products for a keyword
My code:
$searchquery = "ipod";
$api_endpoint = "http://api.walmartlabs.com/v1/search";
$postfields = "apiKey=". $appid ."&query=" . $searchquery;
//$postfields = array('apiKey' => $appid, 'query' => $searchquery);
$connection = curl_init();
curl_setopt($connection, CURLOPT_URL, $api_endpoint);
curl_setopt($connection, CURLOPT_SSL_VERIFYPEER, false);
//curl_setopt($connection, CURLOPT_SSL_VERIFYHOST, 0);
//curl_setopt($connection, CURLOPT_HTTPHEADER, $headers);
curl_setopt($connection, CURLOPT_POST, true);
curl_setopt($connection, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($connection, CURLOPT_HEADER, true);
//curl_setopt($connection, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($connection, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($connection);
curl_close($connection);
print_r($api_endpoint);
print_r($response);
When i go in browser and visit api.walmartlabs.com/v1/search?apiKey={appid}&query=ipod , it shows results, but when i try to do with curl , it shows
"Action Not Found"
Here is the Screenshot
Any help would be appreciated.
looking on the doc (https://developer.walmartlabs.com/io-docs) it appears that the server expects a GET request.
Just replace your POST request with a GET request and all should be fine
$searchquery = "ipod";
$api_endpoint = "http://api.walmartlabs.com/v1/search";
$urlParams = "apiKey=". $appid ."&query=" . $searchquery;
$fullUrl = $api_endpoint . '?' . $urlParams;
$connection = curl_init();
curl_setopt($connection, CURLOPT_URL, $fullUrl);
curl_setopt($connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($connection, CURLOPT_HEADER, true);
curl_setopt($connection, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($connection);
curl_close($connection);
print_r($api_endpoint);
print_r($response);
<?php
ini_set('display_errors', 1);
//API URL : https://affiliates.walmart.com/#!/api
$api_key="**********"; //https://developer.walmartlabs.com/apps/mykeys
$keywords="men%20watches"; // Search text - whitespace separated sequence of keywords to search for
$format="json"; // data we want in response
$responseGroup="base"; // Specifies the item fields returned in the response, allowed response groups are [base, full]. Default value is base.
$sort='price'; //Sorting criteria, allowed sort types are [relevance, price, title, bestseller, customerRating, new]. Default sort is by relevance.
$order="asc"; //Sort ordering criteria, allowed values are [asc, desc]. This parameter is needed only for the sort types [price, title, customerRating].
//http://api.walmartlabs.com/v1/search?apiKey={apiKey}&lsPublisherId={Your LinkShare Publisher Id}&query=ipod
$request_url="http://api.walmartlabs.com/v1/search?apiKey=".$api_key."&query=".$keywords."&format=".$format."&responseGroup=".$responseGroup."&sort=".$sort."&order=".$order;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$request_url);
curl_setopt($ch, CURLOPT_FAILONERROR,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$retValue = curl_exec($ch);
// Check for errors and display the error message
if($errno = curl_errno($ch)) {
$error_message = curl_strerror($errno);
echo "cURL error ({$errno}):\n {$error_message}";
}
curl_close($ch);
$arr = json_decode($retValue,true);
echo "<pre>";
print_r($arr);
// echo "<pre>";
// var_dump($retValue);
?>

PHP - echo json off url

I am trying to echo json of of a url. I can't figure out how to do it. I have tried many things. Can someone tell me why my code is not functioning properly? No json gets echoed to the screen. Thanks! Here is the link http://www.eastbay.com/shoppingcart/gateway?action=requestKey&_=
<?php
$raw_json = file_get_contents('http://www.eastbay.com/shoppingcart/gateway?action=requestKey&_=');
$json_array = json_decode($raw_json, true);
echo $json_array;
?>
Its not very pretty, you could put this in a loop but it wil get you started, I hope. Only thing you need to do is change the path where you want to store the cookies, it must be an absolute path.
<?php
$cookies = 'C:/folder/folder..../cookies.txt'; //use a absolute path
//Part 1 to get the cookies
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "http://www.eastbay.com");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookies); // Send Cookies.
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies); // Receive Cookies.
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
//part 2 to get the json
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "http://www.eastbay.com/shoppingcart/gateway?action=requestKey&_=");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookies); // Send Cookies.
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies); // Receive Cookies.
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
// Decoding the json in to an array
$decoded = json_decode($output, true);
//using the array to get the data
echo "RequestKey: " . $decoded['data']['RequestKey'] . "<br>";
echo "Success: ". $decoded['success'] . "<br>";
//foreach loop because its an array which hold the errors, it wont output anything because there are no errors.
foreach($decoded['errors'] AS $error){
echo "Error: " . $error . "<br>";
}
?>

cURL can't get content of specific website

I try to get the content of this website with cURL
www.mytischtennis.de/public/
but it gets no body response. With many other websites the code works:
<?php
$output = grabPage(
"http://www.mytischtennis.de/public/"
//"http://www.spiegel.de" //this page and many other pages are working
);
if (is_array($output)) {
var_dump($output);
} else {
echo $output;
}
function grabPage($url)
{
$ch = curl_init();
$cookiePath= dirname(__FILE__) . "\cookie.txt";
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_MAXREDIRS, 50);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_TIMEOUT, 40);
curl_setopt($ch, CURLOPT_COOKIE, 'CFID=c7a592d8-5798-4471-9af4-4c4d954d03cd; cfid=c7a592d8-5798-4471-9af4-4c4d954d03cd; MYTT_COOKIESOK=1; CFTOKEN0=; cftoken=0; SRV=74');
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiePath);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiePath);
$fpErrors = fopen(dirname(__FILE__) . '\errorlog.txt', 'w');
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_STDERR, $fpErrors);
curl_setopt($ch, CURLOPT_URL, $url);
ob_start();
$curl_exec = curl_exec($ch);
ob_end_clean();
if ($curl_exec === false) {
echo 'Error: ' . curl_error($ch);
} else {
echo 'Success';
}
var_dump(curl_getinfo($ch));
curl_close($ch);
return $curl_exec;
}
I tried to read a fiddler/wireshark dump of a browser request to this website. But I can't figure out which of that many requests and which parameters are necessary to get the content.
You can test cURL with the url www.mytischtennis.de/public/ also on this website:
http://onlinecurl.com/
You need to accept gzip encoding in the response by sending the appropriate HTTP header in the request:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept-Encoding: gzip'));
Now your answer from the server might or might not be gziped. The proper way to check that is to interpret the Content-Encoding HTTP header in the response. But you can also do it quick and dirty like this:
$content = #gzdecode($curl_exec);
return $content !== false ? $content : $curl_exec;

getting JSON object from a GET cURL in php

Here is my code:
$ch = curl_init('');
$request = 'where={"place":"'.$place.'"}&count=1&limit=0';
$url = "https://api.parse.com/1/classes/className" . "?" . $request;
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X-Parse-Application-Id: ...',
'X-Parse-REST-API-Key: ...
));
//curl_setopt($ch, CURLOPT_POST, true);
// Execute
$result = curl_exec($ch);
// Close connection
curl_close($ch);
Here is the output:
{"results":[],"count":0}
I want to get only he number in count.
I tried this:
$json_res = json_decode($result, true);
echo 'Number : '.$json_res['count'];
but the output becomes:
Number :
If i do json_encode the result is just a True value.
I tried added this:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
but didn't change anything.
What am I missing?
EDIT: doing a var_dump on $json_res gives me that: int(1).
Why is that?
Curl does not return the output if you don't set the CURLOPT_RETURNTRANSFER option
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

Categories