I'm trying to write the array $jsonDataInArray to an external csv file. Right now, my file only has the column headers without the data underneath. Could someone help me step through this PHP array, $jsonDataInArray, and write it to an external .csv file?
//set url for pipedrive data being pulled
$api_token="soemToken";
$url = "https://someURL.com;
$ch = curl_init(); //initialize connection with a URL
//check if cURL is enabled or not
if(is_callable('curl_init'))
{
echo "curl_init Enabled";
}
else
{
echo "curl_init Not enabled";
}
echo '<br/><br/><br/><br/><br/><br/>';
curl_setopt($ch, CURLOPT_URL, $url); //fetching URL
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //return queried data as string
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);//verify certificate
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);//check the existence of a common name & verify that it matches the hostname provided
curl_setopt ($ch, CURLOPT_CAINFO, dirname(__FILE__)."/permissingFile.pem");//name of file holding certificates to verify peer with
$json_response = curl_exec($ch);//perform cURL session. Returns ALL of JSON data if sucessful, false if not.
$info = curl_getinfo($ch);//gets array of info about cURL transfer.
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);//gets HTTP message about cURL transfer
if ( $status != 200 )
{
die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($ch) . ", curl_errno " . curl_errno($ch));
//die();
}
curl_close($ch);//close connection with URL
// create an array from the data that is sent back from the API
$response = json_decode($json_response, 1);
// Gets the count of records returned from the api. Used in the for loop to go through response 1 array element at a time.
$count = Count($response['data']);
for ($x=0; $x<$count; $x++)
{
$currentRecord = $response['data'][$x];
$jsonDataInArray = array
(
"id" => $response['data'][$x]['id'],
"user_id" => $response['data'][$x]['user_id']['id'],
"person_id" => $response['data'][$x]['person_id']['value'],
"org_id" => $response['data'][$x]['org_id']['value'],
"stage_id" => $response['data'][$x]['stage_id'],
"title" => $response['data'][$x]['title'],
"value" => $response['data'][$x]['value'],
"currency" => $response['data'][$x]['currency'],
"add_time" => $response['data'][$x]['add_time'],
"update_time" => $response['data'][$x]['update_time'],
"stage_change_time" => $response['data'][$x]['stage_change_time'],
"active" => $response['data'][$x]['active'],
"deleted" => $response['data'][$x]['deleted'],
"status" => $response['data'][$x]['status'],
);
ksort($currentRecord);
}
$test_array = $response['data'][0];//test_array = first row of data
if($startPos == 0){
$fp = fopen('cacheDeals3.csv', 'w');
fputcsv($fp, array_keys($response['data'][0]));
}else{
$fp = fopen('cacheDeals3.csv', 'a');
}
foreach ($jsonDataInArray as $fields)
{
fputcsv($fp, $fields);
}
$jsonDataInArray is being overwritten on every iteration of the for loop, therefore fputcsv is being passed a string as the $field parameter rather than an array.
You need to append a new array to $jsonDataInArray each time, change
$jsonDataInArray = array
to
$jsonDataInArray[] = array
Related
I'm trying to get the details from this example (i created the code right now).
But i'm very... confused... how can i get the details of the link, then separate and send to my MYSQL database..
<?php
$ch = curl_init();
$url = "https://reqres.in/api/users?page=2";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$resp = curl_exec($ch);
if($e = curl_error($ch)) {
echo $e;
}
else {
$decoded = json_decode($resp, true);
//print_r($decoded);
foreach($decoded as $key => $item) {
$array = array(
'id' => ,
'email' => ,
'first_name' => ,
'last_name' => ,
);
print_r($array);
}
}
curl_close($ch);
?>
If you call the url in your browser then you will see that the result array is present in the data field.
You may check this by printing the whole result:
print_r($decoded);
So if you like to print_r the results it should be simply
print_r($decoded['data']);
If you like to store it in your database you may walk through the array and store each item
foreach($decoded['data'] as $item) {
storeItem($item);
}
To make this work you should implement the storeItem function which accepts the array $item and stores it into your database. There are various tutorials about doing that.
So I'm trying to centralize products in one central php file and have my client side php just request info so I only have to edit the central php file to add and remove products
my server side
$varProduct= (
// [0] [1] [2] [3 4 5 6 7] [8]
array("Title" , 0001 , 100, 0,0,1,1,0, "/womens/tops/s/2.png", "/womens/tops/s/2.jpg", "/womens/tops/s/2.jpg", 50 )
)
In my html client side I want to display the title, the price [2] and the url [8]
basically
for(i=o, i< $varProduct.length(), i++){
//display $varProduct[i][0];
//display the Image for $varProduct[i][8];
//display $varProduct[i][2];
}
how can I put values in my server side file to my client side in within html tags? I need to display them inline will I be able to format the variables?
Try something like this
<?php
for ($i = 0; $i < count($varProduct); $i++) {
//full path -- then post pram
$return = sendPostData("http://stackoverflow.com/", array('parm1' => $varProduct[$i][0], 'parm2' => $varProduct[$i][0]));
print_r($return);
}
?>
<?php
//send data function
function sendPostData($url, Array $post) {
$data = "";
foreach ($post as $key => $row) {
$row = urlencode($row); //fix the url encoding
$key = urlencode($key); //fix the url encoding
if ($data == "") {
$data .="$key=$row";
} else {
$data .="&$key=$row";
}
}
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$result = curl_exec($ch);
curl_close($ch); // Seems like good practice
return $result;
}
?>
When i'm trying to invoke the YQL via cURL i'm getting the following error.
HTTP Version Not Supported
Description: The web server "engine1.yql.vip.bf1.yahoo.com" is using an unsupported version of the HTTP protocol.
Following is the code used
// URL
$URL = "https://query.yahooapis.com/v1/public/yql?q=select * from html where url=\"http://www.infibeam.com/Books/search?q=9788179917558\" and xpath=\"//span[#class='infiPrice amount price']/text()\"&format=json";
// set url
curl_setopt($ch, CURLOPT_URL, $URL);
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
echo $output;
?>
Invoking the same URL from thr browser works fine
https://query.yahooapis.com/v1/public/yql?q=select * from html where
url="http://www.infibeam.com/Books/search?q=9788179917558" and
xpath="//span[#class='infiPrice amount price']/text()"&format=json
Can someone please point me what is wrong in the code?
The problem is probably caused because the url you feed to cURL is not valid. You need to prepare / encode the individual values of the query strings for use in a url.
You can do that using urlencode():
$q = urlencode("select * from html where url=\"http://www.infibeam.com/Books/search?q=9788179917558\" and xpath=\"//span[#class='infiPrice amount price']/text()\"");
$URL = "https://query.yahooapis.com/v1/public/yql?q={$q}&format=json";
In this case I have only encoded the value of q as the format does not contain characters that you cannot use in a url, but normally you'd do that for any value you don't know or control.
Okay I gottacha .. The problem was with the https. Used the following snippet for debug
if (false === ($data = curl_exec($ch))) {
die("Eek! Curl error! " . curl_error($ch));
}
Added below code to accept SSL certificates by default.
$options = array(CURLOPT_URL => $URL,
CURLOPT_HEADER => "Content-Type:text/xml",
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_RETURNTRANSFER => TRUE
);
Complete code is here
<?php
// create curl resource
$ch = curl_init();
// URL
$q = urlencode("select * from html where url=\"http://www.infibeam.com/Books/search?q=9788179917558\" and xpath=\"//span[#class='infiPrice amount price']/text()\"");
$URL = "https://query.yahooapis.com/v1/public/yql?q={$q}&format=json";
echo "URL is ".$URL;
$ch = curl_init();
//Define curl options in an array
$options = array(CURLOPT_URL => $URL,
CURLOPT_HEADER => "Content-Type:text/xml",
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_RETURNTRANSFER => TRUE
);
//Set options against curl object
curl_setopt_array($ch, $options);
//Assign execution of curl object to a variable
$data = curl_exec($ch);
echo($data);
//Pass results to the SimpleXMLElement function
//$xml = new SimpleXMLElement($data);
echo($data);
if (false === ($data = curl_exec($ch))) {
die("Eek! Curl error! " . curl_error($ch));
}
if (200 !== (int)curl_getinfo($ch, CURLINFO_HTTP_CODE)) {
die("Oh dear, no 200 OK?!");
}
//Close curl object
curl_close($ch);
?>
I am trying to connect to a CRM (Pardot).
I have this to create the URL necessary to call the XML:
//this will log in and print your API Key (good for 1 hour) to the console
$rz_key = callPardotApi('https://pi.pardot.com/api/login/version/3',
array(
'email' => 'myemail#email.com',
'password' => 'password',
'user_key' => '032222222222222b75a192daba28d'
)
);
$number_url = 'https://pi.pardot.com/api/prospect/version/3/do/query';
$number_url .= '?user_key=032222222222222b75a192daba28d';
$number_url .= '&api_key=';
$number_url .= trim($rz_key);
$number_url .= '&list_id=97676';
$ike = simplexml_load_file($number_url);
print_r($ike);
Now this code returns :
SimpleXMLElement Object ( [#attributes] => Array ( [stat] => fail [version] => 1.0 ) [err] => Invalid API key or user key )
However, if I echo $number_url, and copy and paste that URL into my browser, it loads wonderfully. If I copy and paste the same echoed URL into simplexml_load_file it works wonderfully also. I MUST use a variable, because the API key is only good for one hour. Any ideas?
The rest of the code is here, which was provided by Pardot :
<?php
/**
* Call the Pardot API and get the raw XML response back
*
* #param string $url the full Pardot API URL to call, e.g. "https://pi.pardot.com/api/prospect/version/3/do/query"
* #param array $data the data to send to the API - make sure to include your api_key and user_key for authentication
* #param string $method GET", "POST", "DELETE"
* #return string the raw XML response from the Pardot API
* #throws Exception if we were unable to contact the Pardot API or something went wrong
*/
function callPardotApi($url, $data, $method = 'GET')
{
// build out the full url, with the query string attached.
$queryString = http_build_query($data, null, '&');
if (strpos($url, '?') !== false) {
$url = $url . '&' . $queryString;
} else {
$url = $url . '?' . $queryString;
}
$curl_handle = curl_init($url);
// wait 5 seconds to connect to the Pardot API, and 30
// total seconds for everything to complete
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($curl_handle, CURLOPT_TIMEOUT, 30);
// https only, please!
curl_setopt($curl_handle, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
// ALWAYS verify SSL - this should NEVER be changed. 2 = strict verify
curl_setopt($curl_handle, CURLOPT_SSL_VERIFYHOST, 2);
// return the result from the server as the return value of curl_exec instead of echoing it
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
if (strcasecmp($method, 'POST') === 0) {
curl_setopt($curl_handle, CURLOPT_POST, true);
} elseif (strcasecmp($method, 'GET') !== 0) {
// perhaps a DELETE?
curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, strtoupper($method));
}
$pardotApiResponse = curl_exec($curl_handle);
if ($pardotApiResponse === false) {
// failure - a timeout or other problem. depending on how you want to handle failures,
// you may want to modify this code. Some folks might throw an exception here. Some might
// log the error. May you want to return a value that signifies an error. The choice is yours!
// let's see what went wrong -- first look at curl
$humanReadableError = curl_error($curl_handle);
// you can also get the HTTP response code
$httpResponseCode = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE);
// make sure to close your handle before you bug out!
curl_close($curl_handle);
throw new Exception("Unable to successfully complete Pardot API call to $url -- curl error: \"".
"$humanReadableError\", HTTP response code was: $httpResponseCode");
}
// make sure to close your handle before you bug out!
curl_close($curl_handle);
return $pardotApiResponse;
}
//this will log in and print your API Key (good for 1 hour) to the console
$rz_key = callPardotApi('https://pi.pardot.com/api/login/version/3',
array(
'email' => 'myemail#email.com',
'password' => 'myPassword',
'user_key' => '********************'
)
);
$number_url = 'https://pi.pardot.com/api/prospect/version/3/do/query';
$number_url .= '?user_key=****************';
$number_url .= '&api_key=';
$number_url .= $rz_key;
$number_url .= '&list_id=97676';
$number_url = preg_replace('/\s+/', '', $number_url);
$ike = simplexml_load_file($number_url);
print_r($ike);
?>
There is a redirect to server for information and once response comes from server, I want to check HTTP code to throw an exception if there is any code starting with 4XX. For that I need to know how can I get only HTTP code from header? Also here redirection to server is involved so I afraid curl will not be useful to me.
So far I have tried this solution but it's very slow and creates script time out in my case. I don't want to increase script time out period and wait longer just to get an HTTP code.
Thanks in advance for any suggestion.
Your method with get_headers and requesting the first response line will return the status code of the redirect (if any) and more importantly, it will do a GET request which will transfer the whole file.
You need only a HEAD request and then to parse the headers and return the last status code. Following is a code example that does this, it's using $http_response_header instead of get_headers, but the format of the array is the same:
$url = 'http://example.com/';
$options['http'] = array(
'method' => "HEAD",
'ignore_errors' => 1,
);
$context = stream_context_create($options);
$body = file_get_contents($url, NULL, $context);
$responses = parse_http_response_header($http_response_header);
$code = $responses[0]['status']['code']; // last status code
echo "Status code (after all redirects): $code<br>\n";
$number = count($responses);
$redirects = $number - 1;
echo "Number of responses: $number ($redirects Redirect(s))<br>\n";
if ($redirects)
{
$from = $url;
foreach (array_reverse($responses) as $response)
{
if (!isset($response['fields']['LOCATION']))
break;
$location = $response['fields']['LOCATION'];
$code = $response['status']['code'];
echo " * $from -- $code --> $location<br>\n";
$from = $location;
}
echo "<br>\n";
}
/**
* parse_http_response_header
*
* #param array $headers as in $http_response_header
* #return array status and headers grouped by response, last first
*/
function parse_http_response_header(array $headers)
{
$responses = array();
$buffer = NULL;
foreach ($headers as $header)
{
if ('HTTP/' === substr($header, 0, 5))
{
// add buffer on top of all responses
if ($buffer) array_unshift($responses, $buffer);
$buffer = array();
list($version, $code, $phrase) = explode(' ', $header, 3) + array('', FALSE, '');
$buffer['status'] = array(
'line' => $header,
'version' => $version,
'code' => (int) $code,
'phrase' => $phrase
);
$fields = &$buffer['fields'];
$fields = array();
continue;
}
list($name, $value) = explode(': ', $header, 2) + array('', '');
// header-names are case insensitive
$name = strtoupper($name);
// values of multiple fields with the same name are normalized into
// a comma separated list (HTTP/1.0+1.1)
if (isset($fields[$name]))
{
$value = $fields[$name].','.$value;
}
$fields[$name] = $value;
}
unset($fields); // remove reference
array_unshift($responses, $buffer);
return $responses;
}
For more information see: HEAD first with PHP Streams, at the end it contains example code how you can do the HEAD request with get_headers as well.
Related: How can one check to see if a remote file exists using PHP?
Something like:
$ch = curl_init();
$httpcode = curl_getinfo ($ch, CURLINFO_HTTP_CODE );
You should try the HttpEngine Class.
Hope this helps.
--
EDIT
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $your_agent_variable);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $your_referer);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$output = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpcode ...)
The solution you found looks good. If the server is not able to send you the http headers in time your problem is that the other server is broken or under very heavy load.