How to use php curl to get contacts from icontact? - php

I am trying to get the list of contacts for my account in iContact using a php script. I can get the contact list using RestClient by entering the set of headers, the url and pressing go so I'm pretty sure my headers and url are correct. iContact provides example code for this task however when I run that (with my account and ap details) I get the same response as when I run the following code:
<?php
$Headers = array(
"Accept: text/xml",
"Content-Type: text/xml",
"API-Version: 2.2",
"API-AppId: grBddgWuirhAYT41K6gvrvRGaUGJFVQL",
"API-Username: <removed>",
"API-Password: <removed>");
$Url = "https://app.sandbox.icontact.com/icp/a/412608/c/123920/contacts";
$Handle = curl_init();
if(!$Handle)
{
die("Could not create a cURL handle.");
}
curl_setopt($Handle, CURLOPT_URL, $Url);
curl_setopt($Handle, CURLOPT_HTTPHEADER, $Headers);
curl_setopt($Handle, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.15) Gecko/20110303 Ubuntu/10.04 (lucid) Firefox/3.6.15");
curl_setopt($Handle, CURLOPT_RETURNTRANSFER, true);
$Response = curl_exec($Handle);
$iCode = curl_getinfo($Handle);
echo "Response: <br />";
print_r($Response);
echo "<br /><br /><br /><br /><br /><br /><br /><br /><br /><br />";
echo "Code: <br />";
print_r($iCode);
curl_close($Handle);
?>
This gives the out put:
Response:
Code:
Array ( [url] => https://app.sandbox.icontact.com/icp/a/412608/c/123920/contacts [content_type] => [http_code] => 0 [header_size] => 0 [request_size] => 0 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.094 [namelookup_time] => 0 [connect_time] => 0.094 [pretransfer_time] => 0 [size_upload] => 0 [size_download] => 0 [speed_download] => 0 [speed_upload] => 0 [download_content_length] => -1 [upload_content_length] => -1 [starttransfer_time] => 0 [redirect_time] => 0 )
From what I understand of curl (which isn't very much at the moment) the http_code should be 200 and the xml should be in $Response.
Could anyone point out what I'm doing wrong please?
The iContact examples are available here:
http://developer.icontact.com/documentation/code-library-zip-file/
I am trying to do what get_contacts.php should do.

The problem wasn't with the code, that was fine.
I couldn't run it and get a response from my localhost though, had to use a public server.

Related

PHP curl rest client works when running on command line but not in webpage

I have written a standalone php script which make a curl post call. The script works fine from command line (run as php script from netbean IDE). But when I put the same code inside php controller and invoke the controller from webpage, it failed. The following is the code:
$data = '{"active":"true","state":["2" ,"1"],
"cat_item_Name":"IT Network Data Center",
"limit":2500,
"fields":["request","cat_item","number","active","state"],
"variables":"true",
"children":"true",
childOptions":{"limit":100,
"fields":["number","description","active","assignment_group","assigned_to"]
}}';
//decoded json object
$jsonDataObject = json_decode($data, true);
try {
$base_url = 'https://anotherwebsite;
$curl = curl_init($base_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); //$jsonDataObject);
$headers = array(
'Content-Type:application/json',
'Authorization: Basic '. base64_encode("username:password")
);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
//curl_setopt($curl, CURLOPT_USERPWD, "username:password");
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$curl_response = curl_exec($curl);
if ($curl_response === false) {
$info = curl_getinfo($curl);
curl_close($curl);
die('error occured during curl exec. Additioanl info: ' . var_export($info));
}
close($curl);
the output for the failed request is:
Array
(
[url] => https://anotherwebsite
[content_type] =>
[http_code] => 0
[header_size] => 0
[request_size] => 0
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0
[namelookup_time] => 0.224197
[connect_time] => 0.278377
[pretransfer_time] => 0
[size_upload] => 0
[size_download] => 0
[speed_download] => 0
[speed_upload] => 0
[download_content_length] => 0
[upload_content_length] => 0
[starttransfer_time] => 0
[redirect_time] => 0
)
What can be the reason it fails when being invoked from web page (e.g. a button which calls the action function), whereas it works when running the standalone script from command line?
Thanks in advance!
Like Mohsin has said, you have missed a single quote in the $base_url variable (line 13 of your example code). So in your case, the syntax is incorrect. You can use php -l filename.php to check if the syntax is correct for a specified PHP file.

Curl http_code 400

I am trying to send the push Notification but it is getting problem
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $id,
'data' => $load,
);
$headers = array(
'Content-Type: application/json',
'Authorization: key=' . GOOGLE_API_KEY,
);
// Open connection
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
//curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11');
// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields, true));
// Execute post
$result = curl_exec($ch);
$httpcode = curl_getinfo($ch);
And when I try to see the curl info I see there is http_code 400. I did everything but still I am getting problem and push notification is not working.
Can you guys please help me ? I am stuck here.
Array
(
[url] => https://android.googleapis.com/gcm/send
[content_type] => text/plain; charset=UTF-8
[http_code] => 400
[header_size] => 406
[request_size] => 698
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.22549
[namelookup_time] => 0.028427
[connect_time] => 0.030052
[pretransfer_time] => 0.189248
[size_upload] => 382
[size_download] => 41
[speed_download] => 181
[speed_upload] => 1694
[download_content_length] => -1
[upload_content_length] => 382
[starttransfer_time] => 0.225382
[redirect_time] => 0
[certinfo] => Array
(
)
[primary_ip] => 172.217.6.234
[primary_port] => 443
[local_ip] => 162.243.229.189
[local_port] => 54327
[redirect_url] =>
)
Error 400 means Bad Request. I see in your code, in the $headers array that you have the GOOGLE_API_KEY which is not defined in your script. You also have not defined $id and $load from what I can see.
Firstly, you will need a Google Cloud Messaging API Key, which you can obtain from the Google Developer's Console.
Please note that the method which you are employing requires a client side app installed on the mobile device. An alternative would be to send a web-push notification, which pushes the notification to Chrome (or any other compatible web browser), so long as it is running and has any tab open. If this is an option you think is worth looking at, I suggest you look at the following web-push library on Git-Hub.

Trouble POSTing using cURL, PHP, and Basic Authentication

I made a similar post last week, which can be found here. I was having problems with authentication, which I believe I've solved. The error message is different, at least.
I'm trying to recreate a successful POST I made to our vendor via POSTman by executing cURL commands in PHP.
Here is the example cURL command from their documentation:
curl -i -k -u '<api_key>': -XPOST --data-urlencode assessment_data#/path/to/test/file.json "https://<your_subdomain>.vendor.org/api/v1/import"; echo ""
This is my PHP code, which does not work:
<?php session_start();
include('../connect.php');
include('../functions.php');
function curlPost($url, $headers, $username, $password, $post) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CAINFO, 'certificate.pem');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
echo '<pre>';
echo curl_exec($ch) . '<br><br>';
echo print_r(curl_getinfo($ch)) . '<br><br>';
echo curl_error($ch) . '<br><br>';
echo '</pre>';
curl_close($ch);
}
$data = 'JSON_object={"JSON_key":"JSON_value"}';
curlPost('https://company.vendor.org/api/v1/import',
['Content-Type: multipart/form-data'],
'api-key',
'',
$data
);
and instead produces this output:
{"success":false,"errors":["500 Internal Server Error"],"warnings":[],"info":[],"meta":[],"results":[]}
Array
(
[url] => https://company.vendor.org/api/v1/import
[content_type] => application/json; charset=utf-8
[http_code] => 500
[header_size] => 547
[request_size] => 248
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 1.586493
[namelookup_time] => 0.135777
[connect_time] => 0.177182
[pretransfer_time] => 0.286958
[size_upload] => 1999
[size_download] => 103
[speed_download] => 64
[speed_upload] => 1260
[download_content_length] => 103
[upload_content_length] => 1999
[starttransfer_time] => 0.345878
[redirect_time] => 0
[redirect_url] =>
[primary_ip] => xx.xxx.xx.xxx
[certinfo] => Array
(
'This array is actually empty, I didn't delete anything.'
)
[primary_port] => 443
[local_ip] => xxx.xxx.xx.xxx
[local_port] => 60532
[request_header] => POST /api/v1/import HTTP/1.1
Authorization: Basic xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Host: company.vendor.org
Accept: */*
Content-Type: multipart/form-data
Content-Length: 1999
Expect: 100-continue
)
1
For good measure, here is a screenshot of a successful POST using POSTman (It's censored a little differently, but I'm passing in the exact same information):
Their documentation contains an example JSON object, which they save as a file and send as form-data. I've managed to recreate the object with live data, output it to a properly formatted string, and send that as the value instead. This works in POSTman, but not in my PHP, so I suspect the problem lies there somehow. CURLOPT_POSTFIELDS requires a string (tried an array - it got mad), and I've tried formatting it every way I can think of. Any help would be greatly appreciated.

Sending xml with curl to external url "No response"

I'm trying to send xml to a rest server.
$url = url;
$file = 'finn/test.xml';
$post = array('name' => 'fil','file_contents'=>'#'.$file);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
When I run this code I get a blank response.
I know there's nothing wrong with the server or the xml because when I use this to just upload the file to the server everything works fine.
<html>
<body>
<form name="bilupload" action="url" method="post" enctype="multipart/form-data">
partner: <input TYPE="FILE" NAME="fil" size="10">
<br>
<input type="submit" value="Send">
</form>
</body>
</html>
curl_getinfo prints this:
[url] => url
[content_type] => text/html;charset=ISO-8859-1
[http_code] => 200
[header_size] => 497
[request_size] => 144
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.062231
[namelookup_time] => 2.7E-5
[connect_time] => 0.016873
[pretransfer_time] => 0.016928
[size_upload] => 3668
[size_download] => 35
[speed_download] => 562
[speed_upload] => 58941
[download_content_length] => 35
[upload_content_length] => 3668
[starttransfer_time] => 0.034394
[redirect_time] => 0
I've been struggling with this for quite some time and I would be very grateful if anyone could point me in the right direction.
Your form tells the server that it's sending form data in a given format:
enctype="multipart/form-data"
But your PHP scripts tells it's sending XML (but doesn't send XML at all):
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
I suppose you want this:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
... though you can just omit the line.
Perhaps you had the Accept header in mind :-?

Retrieve Twitter's X-RateLimit-Limit in PHP

With every Twitter request I make, the returned HTTP headers should include X-RateLimit-Limit.
However, I seem unable to retrieve these using PHP. Can someone tell me what bone-headed mistake I've made?
I've set my curl up in the normal way and am able to successfully GET and POST requests.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE);
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
$response = curl_exec($ch);
$response_info=curl_getinfo($ch);
$erno = curl_errno($ch);
$er = curl_error($ch);
curl_close($ch);
I'm able to get some response information, like http_code
$response_info['http_code']
But this line just returns null
//Doesn't bloody work. No idea why!
$rate_limit = $response_info['X-RateLimit-Limit'];
I'm running PHP Version 5.3.10.
EDIT
This is the result of print_r($response_info);
Array
(
[url] => https://api.twitter.com/1/statuses/home_timeline.json...
[content_type] => application/json;charset=utf-8
[http_code] => 200
[header_size] => 695
[request_size] => 410
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 1.239977
[namelookup_time] => 0.007361
[connect_time] => 0.155783
[pretransfer_time] => 0.465397
[size_upload] => 0
[size_download] => 99425
[speed_download] => 80182
[speed_upload] => 0
[download_content_length] => 99425
[upload_content_length] => 0
[starttransfer_time] => 0.794829
[redirect_time] => 0
[certinfo] => Array()
[redirect_url] =>
[request_header] => GET /1/statuses/home_timeline.json... HTTP/1.1
Host: api.twitter.com
Accept: */*
)
curl_getinfo does not return the response headers, only other meta info about the request. To retrieve headers, set CURLOPT_HEADER to true. That will include the headers in the output. To separate them from the response body do:
list($headers, $body) = explode("\n\n", $response, 2);
To parse the headers, explode again:
$headers = explode("\n", $headers);
foreach ($headers as $header) {
list($key, $value) = explode(':', $header, 2);
$headers[trim($key)] = trim($value);
}
echo $headers['X-RateLimit-Limit'];

Categories