Related
I have my Laravel PHP application where API is hosted on https://site.example.com and I have Swagger UP API Doc hosted on https://api.example.com. when I try API on Swagger API it gives Access-Control-Allow-Origin Error error. Following is my swagger.json
{
"openapi": "3.0.0",
"info": {
"title": "Seller-Hub",
"description": "",
"license": {
"name": "-"
},
"version": "1.1.0"
},
"servers": [
{
"url": "https://site.example.com/public/api/v1",
"description": "Dev server"
},
],
"paths": {
"/seller": {
"get": {
"tags": [
"Seller"
],
"summary": "Returns Seller Data",
"description": "Returns a map of Seller Data",
"operationId": "seller",
"parameters": [
],
"responses": {
"200": {
"description": "successful operation",
"headers":{
"Access-Control-Allow-Origin":{
"schema":{
"type": "string"
}
},
"Access-Control-Allow-Methods":{
"schema":{
"type": "string"
}
},
"Access-Control-Allow-Headers":{
"schema":{
"type": "string"
}
},
},
"content": {
"application/json": {
"schema": {
}
}
}
},
},
"security": [
{
"ApiSign": []
}
]
}
},
"components": {
"securitySchemes": {
"ApiSign": {
"type": "apiKey",
"in": "header",
"name": "authorization"
}
}
},
}
I am not getting where to pass Access control as headers. I have tried every possible way but no success. any help would be great. Thanks
I need help regarding the returned error after initiating request:
Here's the request body:
{
"name": "Test Leads",
"id": "2",
"type": "Event",
"active": true,
"eventDetails": {
"eventConditions": [
{
"type": "Category",
"comparisonType": "EQUAL",
"expression": "Leads"
},
{
"type": "ACTION",
"comparisonType": "EQUAL",
"expression": "Create"
}
],
"useEventValue": true
}
}
Error:
{"error":{
"errors":[
{"domain":"global","reason":"backendError","message":"Backend Error"}
],
"code":500,
"message":"Backend Error"
}
}
Expected Goal Details when successfully posted:
https://www.screencast.com/t/5QOJ28Tnk
Can someone check the code and see what's wrong or missing?
Thanks.
"name": "Test Leads",
"kind": "analytics#goal",
"id": "2",
"type": "Event",
"active": true,
"eventDetails": {
"eventConditions": [
{
"type": "CATEGORY",
"matchType": "EXACT",
"expression": "Leads"
},
{
"type": "ACTION",
"matchType": "EXACT",
"expression": "Create"
}
],
"useEventValue": true
}
}```
When I run the code below it results in the first row in a long list of columns containing only the text Array.
I've used $decoded = json_decode($json_file, true); to indicate that I want arrays instead of objects so I'm not sure what the issue is.
Should I be using bracket or arrow notation somewhere to specify that I want the array values?
$json_file = file_get_contents('https://fakeurl.com&contentType=json', false);
$decoded = json_decode($json_file, true);
$fp = fopen('output.csv', 'w');
foreach($decoded as $comment) {
fputcsv($fp, $comment);
}
fclose($fp);
Here is a small snippet of the JSON. It looks like this all the way through:
{
"rows": [{
"columns": [{
"name": "clientId",
"value": "1839",
"type": "xs:int",
"format": ""
}, {
"name": "campaignId",
"value": "25646",
"type": "xs:int",
"format": ""
}, {
"name": "campaignStatus",
"value": "Live",
"type": "xs:string",
"format": ""
}, {
"name": "campaignName",
"value": "Template Donation Litle",
"type": "xs:string",
"format": ""
}, {
"name": "campaignExportName",
"value": "Template Donation Litle",
"type": "xs:string",
"format": ""
}, {
"name": "description",
"value": "/donate/template/Litle",
"type": "xs:string",
"format": ""
}]
}, {
"columns": [{
"name": "clientId",
"value": "1839",
"type": "xs:int",
"format": ""
}, {
"name": "campaignId",
"value": "25812",
"type": "xs:int",
"format": ""
}, {
"name": "campaignStatus",
"value": "Live",
"type": "xs:string",
"format": ""
}, {
"name": "campaignName",
"value": "Monthly Only",
"type": "xs:string",
"format": ""
}, {
"name": "campaignExportName",
"value": "Monthly Only",
"type": "xs:string",
"format": ""
}, {
"name": "description",
"value": "A donation receipt will be emailed to the address you submitted. This donation is tax-deductible to the fullest extent of the law.",
"type": "xs:string",
"format": ""
}]
}]
}
Your array is much deeper and the values need to be extracted:
foreach($decoded['rows'] as $row) {
$values = array_column($row['columns'], 'value');
fputcsv($fp, $values);
}
If you want the names/headers in the first row then you need to do this before the loop:
$headers = array_column($decoded['rows'][0]['columns'], 'name');
fputcsv($fp, $headers);
So I have come to a slight issue in phrasing this api response.
I'm trying to phrase the screenshot data from this api response?..
I have tried the below but to no avail!.. It just seems to be NULL?..
any help would be great thanks
$jsonData = json_decode($response, true);
$testt = $jsonData->screenshot->data;
The response:
{
"kind": "pagespeedonline#result",
"id": "http://stackoverflow.com/",
"responseCode": 200,
"title": "Stack Overflow",
"score": 78,
"pageStats": {
"numberResources": 31,
"numberHosts": 11,
"totalRequestBytes": "4412",
"numberStaticResources": 22,
},
"formattedResults": {
"locale": "en_US",
"ruleResults": {
"AvoidLandingPageRedirects": {
"localizedRuleName": "Avoid landing page redirects",
"ruleImpact": 0.0,
"urlBlocks": [
{
"header": {
"format": "Your page has no redirects. Learn more about avoiding landing page redirects.",
"args": [
{
"type": "HYPERLINK",
"value": "https://developers.google.com/speed/docs/insights/AvoidRedirects"
}
]
}
}
]
},
"EnableGzipCompression": {
"localizedRuleName": "Enable compression",
"ruleImpact": 0.0,
"urlBlocks": [
{
"header": {
"format": "You have compression enabled. Learn more about enabling compression.",
"args": [
{
"type": "HYPERLINK",
"value": "https://developers.google.com/speed/docs/insights/EnableCompression"
}
]
}
}
]
},
"LeverageBrowserCaching": {
"localizedRuleName": "Leverage browser caching",
"ruleImpact": 0.0,
"urlBlocks": [
{
"header": {
"format": "You have enabled browser caching. Learn more about browser caching recommendations.",
"args": [
{
"type": "HYPERLINK"
}
]
}
}
]
},
"MainResourceServerResponseTime": {
"localizedRuleName": "Reduce server response time",
"ruleImpact": 0.0,
"urlBlocks": [
{
"header": {
"format": "Your server responded quickly. Learn more about server response time optimization.",
"args": [
{
"type": "HYPERLINK",
"value": "https://developers.google.com/speed/docs/insights/Server"
}
]
}
}
]
},
"MinifyCss": {
"localizedRuleName": "Minify CSS",
"ruleImpact": 0.0,
"urlBlocks": [
{
"header": {
"format": "Your CSS is minified. Learn more about minifying CSS.",
"args": [
{
"value": "https://developers.google.com/speed/docs/insights/MinifyResources"
}
]
}
}
]
},
"MinifyHTML": {
"ruleImpact": 0.1443,
"urlBlocks": [
{
"header": {
"format": "Compacting HTML code, including any inline JavaScript and CSS contained in it, can save many bytes of data and speed up download and parse times."
}
},
{
"header": {
"format": "Minify HTML for the following resources to reduce their size by $2 ($3 reduction).",
"args": [
{
"value": "https://developers.google.com/speed/docs/insights/MinifyResources"
},
{
"value": "1.4KiB"
},
{
"value": "5%"
}
]
},
"urls": [
{
"result": {
"format": "Minifying $1 could save $2 ($3 reduction) after compression.",
"args": [
{
"type": "URL",
"value": "http://stackoverflow.com/"
},
{
"type": "BYTES",
"value": "1.4KiB"
},
{
"value": "5%"
}
]
}
}
]
}
]
},
"MinifyJavaScript": {
"localizedRuleName": "Minify JavaScript",
"ruleImpact": 0.0,
"urlBlocks": [
{
"header": {
"format": "Your JavaScript content is minified. Learn more about minifying JavaScript.",
"args": [
{
"type": "HYPERLINK",
"value": "https://developers.google.com/speed/docs/insights/MinifyResources"
}
]
}
}
]
},
"MinimizeRenderBlockingResources": {
"localizedRuleName": "Eliminate render-blocking JavaScript and CSS in above-the-fold content",
"urlBlocks": [
{
"header": {
"args": [
{
"type": "INT_LITERAL",
"value": "2"
},
{
"type": "INT_LITERAL",
"value": "3"
}
]
}
},
{
"header": {
"format": "None of the above-the-fold content on your page could be rendered without waiting for the following resources to load. Try to defer or asynchronously load blocking resources, or inline the critical portions of those resources directly in the HTML."
}
},
{
"header": {
"args": [
{
"type": "HYPERLINK",
"value": "https://developers.google.com/speed/docs/insights/BlockingJS"
}
]
},
"urls": [
{
"result": {
"format": "$1",
"args": [
{
"type": "URL",
"value": "http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"
}
]
}
},
{
"result": {
"format": "$1",
"args": [
{
"type": "URL",
"value": "http://cdn.sstatic.net/Js/stub.en.js?v=174f553de1d0"
}
]
}
}
]
},
{
"header": {
"format": "Optimize CSS Delivery of the following:",
"args": [
{
"type": "HYPERLINK",
"value": "https://developers.google.com/speed/docs/insights/OptimizeCSSDelivery"
}
]
},
"urls": [
{
"result": {
"format": "$1",
"args": [
{
"type": "URL",
"value": "http://cdn.sstatic.net/stackoverflow/all.css?v=55bbe6171ff2"
}
]
}
},
{
"result": {
"format": "$1",
"args": [
{
"type": "URL",
"value": "http://cdn-careers.sstatic.net/careers/gethired/company.min.css?v=86ee8c8bfa3e"
}
]
}
},
{
"result": {
"format": "$1",
"args": [
{
"type": "URL",
"value": "http://cdn-careers.sstatic.net/careers/gethired/sidebar.min.css?v=88f3fd5e2263"
}
]
}
}
]
}
]
},
"OptimizeImages": {
"localizedRuleName": "Optimize images",
"ruleImpact": 3.0802000000000005,
"urlBlocks": [
{
"header": {
"format": "Properly formatting and compressing images can save many bytes of data."
}
},
{
"header": {
"format": "Optimize the following images to reduce their size by $2 ($3 reduction).",
"args": [
{
"type": "HYPERLINK",
"value": "https://developers.google.com/speed/docs/insights/OptimizeImages"
},
{
"type": "BYTES",
"value": "30KiB"
},
{
"type": "PERCENTAGE",
"value": "42%"
}
]
},
"urls": [
{
"result": {
"format": "Losslessly compressing $1 could save $2 ($3 reduction).",
"args": [
{
"type": "URL",
"value": "http://cdn-careers.sstatic.net/careers/gethired/img/careers4-ad-header-so-crop.png?v=f4ca4345dc81"
},
{
"type": "BYTES",
"value": "16.6KiB"
},
{
"type": "PERCENTAGE",
"value": "73%"
}
]
}
},
{
"result": {
"format": "Losslessly compressing $1 could save $2 ($3 reduction).",
"args": [
{
"type": "URL",
"value": "http://cdn.sstatic.net/img/share-sprite-new.png?v=204b1e0e421b"
},
{
"type": "BYTES",
"value": "2.9KiB"
},
{
"type": "PERCENTAGE",
"value": "19%"
}
]
}
},
{
"result": {
"format": "Losslessly compressing $1 could save $2 ($3 reduction).",
"args": [
{
"type": "URL",
"value": "http://cdn.sstatic.net/Img/sprite-herobox.png?v=d173774f3a9f"
},
{
"type": "BYTES",
"value": "2.8KiB"
},
{
"type": "PERCENTAGE",
"value": "43%"
}
]
}
},
{
"result": {
"format": "Losslessly compressing $1 could save $2 ($3 reduction).",
"args": [
{
"type": "URL",
"value": "http://i.stack.imgur.com/Tfe0K.png"
},
{
"type": "BYTES",
"value": "2.6KiB"
},
{
"type": "PERCENTAGE",
"value": "76%"
}
]
}
},
{
"result": {
"format": "Losslessly compressing $1 could save $2 ($3 reduction).",
"args": [
{
"type": "URL",
"value": "http://i.stack.imgur.com/vobok.png"
},
{
"type": "BYTES",
"value": "2.6KiB"
},
{
"type": "PERCENTAGE",
"value": "76%"
}
]
}
},
{
"result": {
"format": "Losslessly compressing $1 could save $2 ($3 reduction).",
"args": [
{
"type": "URL",
"value": "http://cdn.sstatic.net/stackoverflow/img/sprites.png?v=3c6263c3453b"
},
{
"type": "BYTES",
"value": "1.5KiB"
},
{
"type": "PERCENTAGE",
"value": "9%"
}
]
}
},
{
"result": {
"format": "Losslessly compressing $1 could save $2 ($3 reduction).",
"args": [
{
"type": "URL",
"value": "http://i.stack.imgur.com/G1dzB.png"
},
{
"type": "BYTES",
"value": "962B"
},
{
"type": "PERCENTAGE",
"value": "50%"
}
]
}
}
]
}
]
},
"PrioritizeVisibleContent": {
"localizedRuleName": "Prioritize visible content",
"ruleImpact": 2.0,
"urlBlocks": [
{
"header": {
"format": "Your page requires additional network round trips to render the above-the-fold content. For best performance, reduce the amount of HTML needed to render above-the-fold content."
}
},
{
"header": {
"format": "The entire HTML response was not sufficient to render the above-the-fold content. This usually indicates that additional resources, loaded after HTML parsing, were required to render above-the-fold content. Prioritize visible content that is needed for rendering above-the-fold by including it directly in the HTML response.",
"args": [
{
"type": "HYPERLINK",
"value": "https://developers.google.com/speed/docs/insights/PrioritizeVisibleContent"
}
]
},
"urls": [
{
"result": {
"format": "Only about $1 of the final above-the-fold content could be rendered with the full HTML response $2.",
"args": [
{
"type": "PERCENTAGE",
"value": "26%"
},
{
"type": "SNAPSHOT_RECT",
"value": "snapshot:5"
}
]
}
}
]
}
]
}
}
},
"version": {
"major": 1,
"minor": 15
},
"screenshot": {
"data": "***************8This is the data i need!!*********",
"height": 240,
"mime_type": "image/jpeg",
"width": 320
}
}
You set the assoc Parameter in json_decode on true, so it will return an array!
You just have to delete true like this:
$jsonData = json_decode($response);
$testt = $jsonData->screenshot->data;
Of course you can change true to false aswell:
$jsonData = json_decode($response, false);
$testt = $jsonData->screenshot->data;
More information about this in the PHP-Manual for json_decode
use it like
$jsonData = json_decode($aa, true);
echo $jsonData['screenshot']['data'];
working code
First, there is an error in your JSON on line 11 that stops json_deocde() returning a valid array (you should remove the trailing comma):
"numberStaticResources": 22,
Second, you are getting an array from json_decode() so you need to use the correct syntax:
$jsonData = json_decode($response, true);
$testt = $jsonData['screenshot']['data'];
I need some help with the movie db api. I constantly get a connection timed out error. Below is my code, I'm trying to just output the raw json respone so I can work my way from there.
$header_opt = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept: application/json\r\n" .
"Content-Type: application/json\r\n"
)
);
$headers = stream_context_create($header_opt);
$rawjson = file_get_contents('http://api.themoviedb.org/3/movie/tt0076759?api_key=myapikey', false, $headers);
$cleansjon = json_decode($rawjson);
echo $cleansjon;
And this is the error I get.
Warning: filegetcontents(http://api.themoviedb.org/3/movie/tt0076759?api_key=myapikey) [function.file-get-contents]: failed to open stream: Connection timed out in /*///*/public_html/index.php on line 11
I hope that someone can help me with this.
Edit: Added HAR response
{
"log": {
"version": "1.2",
"creator": {
"name": "WebInspector",
"version": "537.20"
},
"pages": [
{
"startedDateTime": "2013-01-27T09:51:02.534Z",
"id": "page_1",
"title": "http://*MyWebsite*/tmdb.php",
"pageTimings": {
"onContentLoad": 60636,
"onLoad": 60635
}
}
],
"entries": [
{
"startedDateTime": "2013-01-27T09:51:02.534Z",
"time": 60197,
"request": {
"method": "GET",
"url": "http://*MyWebsite*/tmdb.php",
"httpVersion": "HTTP/1.1",
"headers": [
{
"name": "DNT",
"value": "1"
},
{
"name": "Accept-Encoding",
"value": "gzip,deflate,sdch"
},
{
"name": "Host",
"value": "*MyHost*"
},
{
"name": "Accept-Language",
"value": "nl-NL,nl;q=0.8,en-US;q=0.6,en;q=0.4"
},
{
"name": "User-Agent",
"value": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.20 (KHTML, like Gecko) Chrome/25.0.1328.0 Safari/537.20"
},
{
"name": "Accept",
"value": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
},
{
"name": "Cache-Control",
"value": "max-age=0"
},
{
"name": "Connection",
"value": "keep-alive"
},
{
"name": "Accept-Charset",
"value": "ISO-8859-1,utf-8;q=0.7,*;q=0.3"
}
],
"queryString": [],
"cookies": [],
"headersSize": 443,
"bodySize": 0
},
"response": {
"status": 200,
"statusText": "OK",
"httpVersion": "HTTP/1.1",
"headers": [
{
"name": "Date",
"value": "Sun, 27 Jan 2013 09:51:02 GMT"
},
{
"name": "Content-Encoding",
"value": "gzip"
},
{
"name": "Server",
"value": "Apache/2"
},
{
"name": "Vary",
"value": "Accept-Encoding,User-Agent"
},
{
"name": "Content-Type",
"value": "text/html"
},
{
"name": "Connection",
"value": "Keep-Alive"
},
{
"name": "Keep-Alive",
"value": "timeout=1, max=100"
},
{
"name": "Content-Length",
"value": "260"
}
],
"cookies": [],
"content": {
"size": 342,
"mimeType": "text/html",
"compression": 82
},
"redirectURL": "",
"headersSize": 234,
"bodySize": 260
},
"cache": {},
"timings": {
"blocked": 0,
"dns": 13,
"connect": 29,
"send": 0,
"wait": 60147,
"receive": 5,
"ssl": -1
},
"pageref": "page_1"
}
]
}
}
You can remove the Content-Type header, this would be required if you submit JSON using HTTP PUT or POST.
The error indicates, that file_get_contents does not get a response from the host api.themoviedb.org
I constantly get a connection timed out error.
Does constantly mean always or often?
If you see this frequently either your Internet uplink or the movie db server has a problem.
Maybe there is no problem at all. Your application is trying to connect to a server and gets a timeout error.